-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindingACentroid.cpp
More file actions
57 lines (48 loc) · 1.08 KB
/
FindingACentroid.cpp
File metadata and controls
57 lines (48 loc) · 1.08 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
//Finding a Centroid - https://cses.fi/problemset/task/2079
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
void solve() {
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> sz(n, 0);
function<void(int, int)> dfs1 = [&](int u, int p) -> void {
sz[u] = 1;
for (auto &v: g[u]) {
if (v == p) continue;
dfs1(v, u);
sz[u] += sz[v];
}
};
dfs1(0, -1);
function<int(int, int)> dfs2 = [&](int u, int p) -> int {
for (auto &v: g[u]) {
if (v == p) continue;
if (2 * sz[v] > n) {
return dfs2(v, u);
}
}
return u;
};
cout << dfs2(0, -1) + 1 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}