-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree Distances II.cpp
More file actions
51 lines (44 loc) · 1.17 KB
/
Tree Distances II.cpp
File metadata and controls
51 lines (44 loc) · 1.17 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
// Re-Rooting DP
#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 ll N=2e5+5 ;
vector<ll> v[N] ;
ll n,ans[N],subdist[N]={},subsize[N]={} ;
// subsize[i] represents sizeof subtree rooted with i.
// subdist[i] represents total distance of a subtree rooted with i (distance for i only).
void DFS(ll x,ll p){
subsize[x]=1;
for(auto i : v[x]){
if (i!=p){
DFS(i,x) ;
subsize[x]+=subsize[i] ;
subdist[x]+=subdist[i]+subsize[i] ;
}
}
}
void DFS1(ll x,ll p){
ans[x] = (ans[p]-subsize[x]-subdist[x])+(n-subsize[x])+subdist[x] ;
for(auto i : v[x]){
if (i!=p)
DFS1(i,x) ;
}
}
int main() {
IOS ;
ll x,y ;
cin>>n ;
for(ll i=1 ; i<n ; i++){
cin>>x>>y ;
v[y].push_back(x) ;
v[x].push_back(y) ;
}
DFS(1,-1) ;
ans[1]=subdist[1] ; // ans[1] is sum of the distances from the node 1 to all other nodes.
for(auto i : v[1])
DFS1(i,1) ;
for(ll i=1 ; i<=n ; i++) cout<<ans[i]<<" " ;
return 0;
}