-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcentroid.cpp
More file actions
76 lines (58 loc) · 1.4 KB
/
centroid.cpp
File metadata and controls
76 lines (58 loc) · 1.4 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
68
69
70
71
72
73
74
75
76
// BOJ 7045 Tree Cutting
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int dfs(int cur, int prv, vector<vector<int>> &graph, vector<int> &parent, vector<int> &sub) {
parent[cur] = prv;
sub[cur]++;
for (int nxt : graph[cur])
if (nxt != prv)
sub[cur] += dfs(nxt, cur, graph, parent, sub);
return sub[cur];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<vector<int>> graph(n + 1);
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
vector<int> parent(n + 1);
vector<int> sub(n + 1);
dfs(1, 0, graph, parent, sub);
vector<int> ans;
int cur = 1;
while (cur) {
int total = 1;
int mx = 0;
int idx = 0;
for (int nxt : graph[cur]) {
if (nxt == parent[cur])
continue;
total += sub[nxt];
if (mx < sub[nxt]) {
mx = sub[nxt];
idx = nxt;
}
}
if (n - total > n / 2)
break;
if (mx <= n / 2)
ans.push_back(cur);
cur = idx;
}
sort(ans.begin(), ans.end());
for (int x : ans)
cout << x << '\n';
}