-
Notifications
You must be signed in to change notification settings - Fork 0
/
10121.cpp
93 lines (82 loc) · 1.5 KB
/
10121.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
/**
* Use union-find set to find components in the graph.
* Choose one point, Using bfs to get one longest point, and from that point using bfs again to get all of the deepest roots.
*/
#define MAX 10005
int parent[MAX];
vector< int > adj[MAX];
int dist[MAX];
int n;
int find(int a)
{
while(a != parent[a])
a = parent[a];
return a;
}
void merge(int a, int b)
{
parent[a] = b;
}
int bfs(int start)
{
for (int i = 1; i <= n; i++)
dist[i] = -1;
queue<int> q;
q.push(start);
dist[start] = 0;
int maxDist = 0;
while(q.size()){
int current = q.front();
for(int i = 0; i != adj[current].size(); i++){
int tmp = adj[current][i];
if (dist[tmp] < 0){
q.push(tmp);
dist[tmp] = dist[current] + 1;
if (dist[tmp] > maxDist)
maxDist = dist[tmp];
}
}
q.pop();
}
return maxDist;
}
int main()
{
scanf("%d",&n);
int tmp = n + 1;
while(tmp-- != 1)
parent[tmp] = tmp;
int a,b;
for (int i = 1; i < n; ++i){
scanf("%d%d",&a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
int ap = find(a);
int bp = find(b);
if (ap != bp)
merge(ap, bp);
}
int count = 0;
for (int i = 1; i <= n; ++ i)
if (parent[i] == i)
count ++;
if (count > 1){
printf("Error: %d components\n", count);
}else {
int max = bfs(1);
int i;
for (i = 1; i <= n; i++)
if (dist[i] == max)
break;
max = bfs(i);
dist[i] = max;
for (i = 1; i <= n; ++i)
if (dist[i] == max)
printf("%d\n", i);
}
return 0;
}