Skip to content

Commit 87ee17e

Browse files
authored
BOJ #18126: 너구리 구구
1 parent 692cbc9 commit 87ee17e

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

BOJ/18126/Main.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Author: Minho Kim (ISKU)
3+
* Date: November 25, 2019
4+
* E-mail: minho.kim093@gmail.com
5+
*
6+
* https://github.com/ISKU/Algorithm
7+
* https://www.acmicpc.net/problem/18126
8+
*/
9+
10+
import java.io.*;
11+
import java.util.*;
12+
13+
public class Main {
14+
15+
private static List<Edge>[] graph;
16+
private static boolean[] visited;
17+
private static int N;
18+
private static long answer;
19+
20+
public static void main(String[] args) throws Exception {
21+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
22+
N = Integer.parseInt(br.readLine());
23+
24+
graph = new ArrayList[N + 1];
25+
for (int i = 1; i <= N; i++)
26+
graph[i] = new ArrayList<Edge>();
27+
for (int i = 1; i < N; i++) {
28+
StringTokenizer st = new StringTokenizer(br.readLine());
29+
int u = Integer.parseInt(st.nextToken());
30+
int v = Integer.parseInt(st.nextToken());
31+
int w = Integer.parseInt(st.nextToken());
32+
graph[u].add(new Edge(v, w));
33+
graph[v].add(new Edge(u, w));
34+
}
35+
36+
visited = new boolean[N + 1];
37+
answer = 0;
38+
dfs(1, 0);
39+
40+
System.out.println(answer);
41+
}
42+
43+
private static void dfs(int u, long dist) {
44+
visited[u] = true;
45+
if (u != 1 && graph[u].size() == 1)
46+
answer = Math.max(answer, dist);
47+
48+
for (Edge v : graph[u])
49+
if (!visited[v.vertex])
50+
dfs(v.vertex, dist + v.weight);
51+
}
52+
53+
private static class Edge {
54+
public int vertex;
55+
public long weight;
56+
57+
public Edge(int vertex, long weight) {
58+
this.vertex = vertex;
59+
this.weight = weight;
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)