-
Notifications
You must be signed in to change notification settings - Fork 0
/
1021.cpp
68 lines (67 loc) · 1.24 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
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10001;
int n;
vector<int> adj[MAXN];
bool vis[MAXN];
int bfs(int root, bool flag){
if(flag) fill(vis, vis+MAXN, false);
int total = 0, level = 0, thislcnt = 1, nextlcnt = 0;
queue<int> q;
q.push(root);
vis[root] = true;
while(!q.empty()){
int u = q.front();
q.pop();
total++;
for(int i=0; i<adj[u].size(); i++){
int v = adj[u][i];
if(vis[v] == false){
q.push(v);
vis[v] = true;
nextlcnt++;
}
}
thislcnt--;
if(thislcnt == 0){
thislcnt = nextlcnt;
nextlcnt = 0;
level++;
}
}
if(total != n) return -1;
else return level;
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=1; i<n; i++){
int u, v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
set<int> out;
int deepest = 0;
for(int i=1; i<=n; i++){
int deepth = bfs(i, true);
if(deepth == -1){
fill(vis, vis+MAXN, false);
int k = 0;
for(int j=1; j<=n; j++){
if(vis[j] == false){
bfs(j, false);
k++;
}
}
cout<<"Error: "<<k<<" components";
return 0;
}else if(deepth > deepest){
deepest = deepth;
out.clear();
out.insert(i);
}else if(deepth == deepest) out.insert(i);
}
for(auto t : out) cout<<t<<endl;
return 0;
}