-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1021.cpp
More file actions
64 lines (56 loc) · 1.29 KB
/
1021.cpp
File metadata and controls
64 lines (56 loc) · 1.29 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
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
const int N = 10005;
vector<int> graph[N];
vector<bool> marked(N);
void dfs(int v) {
marked[v] = true;
for (auto w : graph[v])
if (!marked[w]) dfs(w);
}
int max_depth;
void dfs(int v, set<int> &s, int depth = 0) {
if (depth > max_depth) {
max_depth = depth;
s.clear();
s.insert(v);
} else if (depth == max_depth) {
s.insert(v);
}
marked[v] = true;
for (auto w : graph[v])
if (!marked[w]) dfs(w, s, depth + 1);
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int v, w;
cin >> v >> w;
graph[v].push_back(w);
graph[w].push_back(v);
}
int count = 0;
fill(marked.begin(), marked.end(), false);
for (int v = 1; v <= n; v++) {
if (marked[v]) continue;
dfs(v);
count++;
}
if (count != 1) {
printf("Error: %d components\n", count);
return 0;
}
set<int> tmp;
fill(marked.begin(), marked.end(), false);
dfs(1, tmp);
set<int> ans(tmp);
fill(marked.begin(), marked.end(), false);
dfs(*ans.begin(), tmp);
for (auto v : tmp) ans.insert(v);
for (auto v : ans) printf("%d\n", v);
return 0;
}