-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCircularRoad_003_02.java
More file actions
57 lines (49 loc) · 1.32 KB
/
LongestCircularRoad_003_02.java
File metadata and controls
57 lines (49 loc) · 1.32 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
package java;
import java.util.*;
import java.util.stream.*;
/**
* 003 - Longest Circular Road(★4)
* 木の直径
* dfs
*/
public class LongestCircularRoad_003_02 {
static int N;
static List<Integer>[] G;
static int[] dist;
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();
dist = new int[N];
dist[0] = 1;
// dfsを用いて、頂点0(1)からの最短距離をdistに記録し、最短距離の最大となる頂点を特定する
dfs(0);
int max = Arrays.stream(dist).max().getAsInt();
int maxId = Arrays.stream(dist).boxed().collect(Collectors.toList()).indexOf(max);
dist = new int[N];
dist[maxId] = 1;
// 頂点max_idから、最短距離の最大値(木の直径)を求める
dfs(maxId);
System.out.println(Arrays.stream(dist).max().getAsInt());
}
/**
* 深さ優先探索
*
* @param pos 開始位置
*/
public static void dfs(int pos) {
for (int i : G[pos]) {
if (dist[i] != 0) continue;
dist[i] = dist[pos] + 1;
dfs(i);
}
}
}