forked from mrsac7/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
2079 - Finding a Centroid.cpp
55 lines (48 loc) · 1.02 KB
/
2079 - Finding a Centroid.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
/*
Problem Name: Finding a Centroid
Problem Link: https://cses.fi/problemset/task/2079
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mxN = 2e5+5;
vector<int> adj[mxN];
int dp[mxN];
void dfs(int s, int p) {
dp[s] = 1;
for (auto i: adj[s]) {
if (i != p) {
dfs(i, s);
dp[s] += dp[i];
}
}
}
int n;
int dfs2(int s, int p) {
pair<int, int> mx = {0, s};
for (auto i: adj[s]) {
if (i != p) {
mx = max(mx, {dp[i], i});
}
}
if (mx.first <= n/2)
return s;
return dfs2(mx.second, s);
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
cin>>n;
for (int i = 1; i < n; i++) {
int x, y; cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0);
cout<<dfs2(1, 0);
}