-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rerooting_in_tree.cpp
58 lines (49 loc) · 1.47 KB
/
Rerooting_in_tree.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
#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
//Rerooting:https://cses.fi/problemset/task/1133
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
if(n==1){
cout<<0;
return 0;
}
vector<vector<int>>adj(n);
for(int i=0;i<n-1;i++){
int x,y; cin>>x>>y;
x--,y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<int>subtree_size(n,0),sum(n,0),ans(n,0);
/* Lambda function in c++ */
function<void(int,int)>dfs=[&](int node,int parent)->void{
subtree_size[node]=1,sum[node]=0;
for(int child:adj[node])if(child!=parent){
dfs(child,node);
subtree_size[node]+=subtree_size[child];
sum[node]+=sum[child]+subtree_size[child];
}
};
auto change=[&](int node,int child){
sum[node]-=(sum[child]+subtree_size[child]);
subtree_size[node]-=subtree_size[child];
subtree_size[child]+=subtree_size[node];
sum[child]+=(sum[node]+subtree_size[node]);
};
function<void(int,int)>reroot=[&](int node,int parent){
ans[node]=sum[node];
for(int child:adj[node])if(child!=parent){
change(node,child);
reroot(child,node);
change(child,node);
}
};
dfs(1,-1);
reroot(1,-1);
for(auto x:ans)cout<<x<<" ";
return 0;
}