forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree Distances II.cpp
More file actions
43 lines (36 loc) · 814 Bytes
/
Tree Distances II.cpp
File metadata and controls
43 lines (36 loc) · 814 Bytes
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 2e5+1;
int N, a, b, sz[maxN];
ll down[maxN], up[maxN];
vector<int> G[maxN];
void dfs1(int u = 1, int p = 0){
sz[u] = 1;
for(int v : G[u]){
if(v != p){
dfs1(v, u);
sz[u] += sz[v];
down[u] += down[v] + sz[v];
}
}
}
void dfs2(int u = 1, int p = 0){
if(p != 0)
up[u] = (up[p]+down[p]) + N - (2*sz[u]+down[u]);
for(int v : G[u])
if(v != p)
dfs2(v, u);
}
int main(){
scanf("%d", &N);
for(int i = 0; i < N-1; i++){
scanf("%d %d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
dfs1();
dfs2();
for(int i = 1; i <= N; i++)
printf("%lld%c", down[i]+up[i], (" \n")[i==N]);
}