-
Notifications
You must be signed in to change notification settings - Fork 0
/
1021.cpp
88 lines (82 loc) · 1.41 KB
/
1021.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
#include <cstdio>
#include <vector>
#include <set>
using namespace std;
const int maxv = 10010;
vector<int> adj[maxv];
int father[maxv];
int find_father(int x){
int a = x;
while(x != father[x]){
x = father[x];
}
while(father[a] != x){
int b = father[a];
father[a] = x;
a = b;
}
return x;
}
void Union(int a, int b){
int fa = find_father(a);
int fb = find_father(b);
if(fa != fb){
father[fa] = fb;
}
}
bool vis[maxv] = {false};
int deepest;
void dfs(int s, int layer){
vis[s] = true;
layer++;
if(layer > deepest){
deepest = layer;
}
for(int i = 0; i < adj[s].size(); i++){
//无向图有回去的边,不能走回头路
if(vis[adj[s][i]] == false){
dfs(adj[s][i], layer);
}
}
vis[s] = false;
}
int main(){
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
father[i] = i;
}
for(int i = 0; i < n - 1; i++){
int u, v;
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
Union(u, v);
}
set<int> st;
for(int i = 1; i <= n; i++){
st.insert(find_father(i));
}
if(st.size() != 1){
printf("Error: %d components\n", st.size());
return 0;
}
set<int> ans;
int Deepest = 0;
for(int i = 1; i <= n; i++){
deepest = 0;
dfs(i, 0);
if(deepest > Deepest){
Deepest = deepest;
ans.clear();
ans.insert(i);
}
else if(deepest == Deepest){
ans.insert(i);
}
}
for(set<int>::iterator it = ans.begin(); it != ans.end(); it++){
printf("%d\n", *it);
}
return 0;
}