-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreroot.cpp
More file actions
53 lines (43 loc) · 885 Bytes
/
Copy pathreroot.cpp
File metadata and controls
53 lines (43 loc) · 885 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
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
typedef long long ll;
typedef long double ld;
const int N = 2e5 + 10;
int n, a[N];
ll sum[N];
vector<int> g[N];
void init(int x, int p = 0){
sum[x] = a[x];
for(int y : g[x]){
if(y == p) continue;
init(y, x);
sum[x] += sum[y];
}
}
void go(int x, int p = 0){
// now, sum[i] is the sum of a[j] such that j is in the subtree of i rooted at x
for(int y : g[x]){
if(y == p) continue;
// root at y
sum[x] -= sum[y];
sum[y] += sum[x];
go(y, x);
// undo root (in other words, root at x)
sum[y] -= sum[x];
sum[x] += sum[y];
}
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 0; i < n - 1; i++){
int u, v; cin >> u >> v;
g[u].pb(v); g[v].pb(u);
}
init(1); // argument to init and go must be the same
go(1);
return 0;
}