-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathA1021.cpp
More file actions
67 lines (62 loc) · 1.07 KB
/
A1021.cpp
File metadata and controls
67 lines (62 loc) · 1.07 KB
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 <cstdio>
#include <vector>
using namespace std;
const int MAXN = 10000 + 10;
int N, depth[MAXN];
bool visited[MAXN];
vector<int> Adj[MAXN];
void init(){
for(int i = 1; i <= N; ++i){
//depth[i] = 0;
visited[i] = false;
}
}
void DFS(int s, int d, int &maxD){
visited[s] = true;
if(d > maxD) maxD = d;
for(int i = 0; i < Adj[s].size(); ++i){
int u = Adj[s][i];
if(visited[u] == false){
DFS(u, d+1, maxD);
}
}
}
int getNumComponets(){
int t, cnt = 0;
for(int i = 1; i <= N; ++i){
if(visited[i] == false){
DFS(i, 0, t);
++cnt;
}
}
return cnt;
}
int main(){
scanf("%d", &N);
for(int i = 0; i < N-1; ++i){
int a, b;
scanf("%d%d", &a, &b);
Adj[a].push_back(b);
Adj[b].push_back(a);
}
init();
int components = getNumComponets();
if(components != 1){
printf("Error: %d components\n", components);
}else{
int maxD = 0;
for(int i = 1; i <= N; ++i){
init();
DFS(i, 0, depth[i]);
if(depth[i] > maxD){
maxD = depth[i];
}
}
for(int i = 1; i <= N; ++i){
if(depth[i] == maxD){
printf("%d\n", i);
}
}
}
return 0;
}