-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCircularRoad_003_01.java
More file actions
68 lines (56 loc) · 1.49 KB
/
LongestCircularRoad_003_01.java
File metadata and controls
68 lines (56 loc) · 1.49 KB
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
package java;
import java.util.*;
import java.util.stream.*;
/**
* 003 - Longest Circular Road(★4)
* 木の直径
* bfs
*/
public class LongestCircularRoad_003_01 {
static int N;
static List<Integer>[] G;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
G = new ArrayList[N];
for (int i = 0; i < N; i++) G[i] = new ArrayList<>();
for (int i = 0; i < N - 1; i++) {
int a = sc.nextInt() - 1;
int b = sc.nextInt() - 1;
G[a].add(b);
G[b].add(a);
}
sc.close();
// 頂点1からの最短距離の最大値となる頂点
int[] ans1 = bfs(0);
//頂点max_idから、木の直径(最短距離の最大値)を求める
int[] ans2 = bfs(ans1[1]);
System.out.println(ans2[0]);
}
/**
* 幅優先探索
* startから各nodeの距離を計測
*
* @param start 開始位置
* @return {最大値, 最終位置}
*/
private static int[] bfs(int start) {
int[] dist = new int[N];
dist[start] = 1;
List<Integer> log = new ArrayList<>();
log.add(start);
while (!log.isEmpty()) {
int from = log.get(0);
log.remove(0);
for (int to : G[from]) {
if (dist[to] != 0) continue;
dist[to] = dist[from] + 1;
log.add(to);
}
}
int max = Arrays.stream(dist).max().getAsInt();
int idx = Arrays.stream(dist).boxed().collect(Collectors.toList()).indexOf(max);
int[] ans = {max, idx};
return ans;
}
}