-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1167.java
76 lines (56 loc) · 1.92 KB
/
1167.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.*;
import java.io.*;
public class Main {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
private static int length;
private static List<Pair>[] edges;
private static boolean[] visited;
public static void main(String[] args) throws IOException {
int V = Integer.parseInt(br.readLine());
edges = new List[V + 1];
for (int i = 0; i < V; i++) {
String[] in = br.readLine().split(" ");
int v = Integer.parseInt(in[0]);
edges[v] = new ArrayList<>();
for (int j = 1; j < in.length - 1; j += 2) {
edges[v].add(new Pair(Integer.parseInt(in[j]), Integer.parseInt(in[j + 1])));
}
}
length = 0;
visited = new boolean[V + 1];
treeLength(1);
bw.write(length + "\n");
bw.close();
}
private static int treeLength(int root) {
if (visited[root])
return 0;
visited[root] = true;
int firstLongest = 0;
int secondLongest = 0;
for (Pair pair : edges[root]) {
if (!visited[pair.next]) {
int l = treeLength(pair.next) + pair.cost;
length = Math.max(length, l);
if (l > firstLongest) {
int temp = firstLongest;
firstLongest = l;
secondLongest = temp;
} else if (l > secondLongest) {
secondLongest = l;
}
}
}
length = Math.max(length, firstLongest + secondLongest);
return firstLongest;
}
private static class Pair {
int next;
int cost;
public Pair(int next, int cost) {
this.next = next;
this.cost = cost;
}
}
}