이 문제는 트리에서 경로에 포함될 수 있는 마을 수의 최댓값을 구합니다. 트리에서는 임의의 두 정점 사이의 경로가 유일하며, 가장 긴 경로를 트리의 지름이라고 합니다.
두 번 탐색하는 방법으로 지름을 구합니다. 임의의 정점에서 시작해 트리를 탐색하고 가장 멀리 있는 끝점 u를 찾은 다음, u에서 다시 탐색합니다. u에서 가장 멀리 있는 정점은 지름의 다른 끝점이므로, 두 번째 탐색에서 얻은 최댓거리가 간선 수 기준 지름의 길이입니다. 정답은 양쪽 끝점을 포함한 정점 수이므로 거리에 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);
}
}