この問題では、木の中の経路に含まれる町の数の最大値を求めます。木では任意の2頂点間の経路が一意に定まり、そのような経路のうち最長のものを木の直径と呼びます。
2回の探索で直径を求めます。任意の頂点から木を探索して最も遠い端点 u を見つけ、次に u から探索します。u から最も遠い頂点は直径のもう一方の端点なので、2回目の探索で得られる最大距離が、辺の本数で表した直径の長さです。答えは両端点を含む頂点数であるため、距離に1を足して出力します。N = 1 の場合、どちらの探索でも最大距離は0となり、出力は 1 です。
各探索ではキューと距離配列を使います。木では各頂点までの経路が一意なので、最初に訪問したときの距離が始点からの経路長です。時間計算量は O(N)、空間計算量は O(N) です。反復処理で実装するため、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
import java.io.*;
import java.util.*;
public class Main {
static int farthest(List<Integer>[] graph, int start, int[] distance) {
Arrays.fill(distance, -1);
int[] queue = new int[graph.length];
int head = 0;
int tail = 0;
queue[tail++] = start;
distance[start] = 0;
int farthest = start;
while (head < tail) {
int town = queue[head++];
if (distance[town] > distance[farthest]) {
farthest = town;
}
for (int next : graph[town]) {
if (distance[next] == -1) {
distance[next] = distance[town] + 1;
queue[tail++] = next;
}
}
}
return farthest;
}
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(input.readLine());
List<Integer>[] graph = new ArrayList[n];
for (int town = 0; town < n; town++) {
graph[town] = new ArrayList<>();
}
for (int i = 0; i < n - 1; i++) {
StringTokenizer edge = new StringTokenizer(input.readLine());
int a = Integer.parseInt(edge.nextToken()) - 1;
int b = Integer.parseInt(edge.nextToken()) - 1;
graph[a].add(b);
graph[b].add(a);
}
int[] distance = new int[n];
int endpoint = farthest(graph, 0, distance);
farthest(graph, endpoint, distance);
int diameterEdges = 0;
for (int d : distance) {
diameterEdges = Math.max(diameterEdges, d);
}
System.out.println(diameterEdges + 1);
}
}