-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZero_Tree.cpp
More file actions
50 lines (45 loc) · 750 Bytes
/
Zero_Tree.cpp
File metadata and controls
50 lines (45 loc) · 750 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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100100;
ll pos[N], neg[N], a[N];
vector<int> adj[N];
int n;
void dfs(int x, int from)
{
pos[x] = neg[x] = 0;
for (auto y : adj[x])
if (y != from)
{
dfs(y, x);
pos[x] = max(pos[x], pos[y]);
neg[x] = max(neg[x], neg[y]);
}
a[x] += neg[x];
a[x] -= pos[x];
if (a[x] > 0)
{
pos[x] += a[x];
a[x] = 0;
}
if (a[x] < 0)
{
neg[x] -= a[x];
a[x] = 0;
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i = 1; i <= n; i++)
cin >> a[i];
dfs(1, 0);
cout << (pos[1] + neg[1]);
return 0;
}