-
Notifications
You must be signed in to change notification settings - Fork 0
/
1021_Deepest_Root.cpp
62 lines (57 loc) · 1.33 KB
/
1021_Deepest_Root.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
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> ans;
vector<bool> visited;
vector<vector<int> > grap;
int max_deep = 0;
void DFS(int node, int level) {
if (max_deep < level) {
ans.clear();
ans.push_back(node);
max_deep = level;
} else if (max_deep == level) {
ans.push_back(node);
}
visited[node] = true;
for (int it : grap[node]) {
if (visited[it] == false) {
DFS(it, level + 1);
}
}
}
int main() {
int v1, v2;
cin >> n;
grap.resize(n + 1);
visited.resize(n + 1, false);
for (int i = 1; i < n; ++i) {
cin >> v1 >> v2;
grap[v1].push_back(v2);
grap[v2].push_back(v1);
}
int count = 0, s1 = 0;
set<int> s;
for (int i = 1; i <= n; ++i) {
if (visited[i] == false) {
DFS(i, 1);
if (i == 1) {
if (ans.size() != 0) s1 = ans[0];
for (int it : ans) s.insert(it);
}
count++;
}
}
if (count > 1) {
cout << "Error: " << count << " components" << endl;
} else {
ans.clear();
max_deep = 0;
visited.clear();
visited.resize(n + 1, false);
DFS(s1, 1);
for (int it : ans) s.insert(it);
for (int it : s) cout << it << endl;
}
return 0;
}