-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinding a Centroid.cpp
More file actions
53 lines (42 loc) · 1.18 KB
/
Finding a Centroid.cpp
File metadata and controls
53 lines (42 loc) · 1.18 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
// We can find a centroid in a tree by starting at the root
// Each step, loopthrough all of its children. (DFS)
// If all of its children have subtree size less than or equal to N/2 then it is a centroid.
// Otherwise, move to the child with a subtree size that is more than N/2 and repeat until you find a centroid.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
const int N=2e5+1 ;
vector<int> adj[N] ;
ll n , subsize[N] ;
void DFS(ll x,ll p){ // for finding subtree size .
subsize[x]=1 ;
for(ll i : adj[x]){
if (i!=p){
DFS(i,x) ;
subsize[x]+=subsize[i] ;
}
}
}
// Return centroid of a Tree
ll get_centroid(ll node, ll p){
for(ll i : adj[node] ){
if (i!=p && subsize[i]>n/2)
node = get_centroid(i,node) ;
}
return node ;
}
int main() {
IOS ;
ll x,y ;
cin>>n ;
for(ll i=1 ; i<n ; i++){
cin>>x>>y ;
adj[x].push_back(y) ;
adj[y].push_back(x) ;
}
DFS(1,-1) ;
cout<<get_centroid(1,-1)<<endl ;
return 0;
}