-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc.cpp
More file actions
executable file
·94 lines (86 loc) · 2.42 KB
/
Copy pathabc.cpp
File metadata and controls
executable file
·94 lines (86 loc) · 2.42 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define anvnh signed main(void)
template <typename T> void print(const T &t) {
for (const auto &element : t) {
std::cout << element << " ";
}
std::cout << std::endl;
}
#define ll long long
#define pb push_back
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define SET_ON(x, i) ((x) | MASK(i))
#define SET_OFF(x, i) ((x) & ~MASK(i))
#define nl "\n"
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define INF 0x3f3f3f3f
const ll MOD = 1e9 + 7;
void dfs(int root, int par, int tot, vector<vector<int>> edges,
vector<int> vertex, int &res) {
int sum = vertex[root];
for (int i = 0; i < (int)edges[root].size(); i++) {
int v = edges[root][i];
if (v != par) {
dfs(v, root, tot, edges, vertex, res);
sum += vertex[v];
}
}
vertex[root] = sum;
if (root != 1 && abs(tot - 2 * sum) < res) {
res = abs(tot - 2 * sum);
}
}
void solve() {
int n;
cin >> n;
vector<int> vertex(n + 1);
vector<vector<int>> edges(n + 1);
int tot = 0;
for (int i = 0; i < n; i++) {
cin >> vertex[i + 1];
tot += vertex[i + 1];
}
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
edges[x].pb(y);
edges[y].pb(x);
}
// for (int i = 1; i <= n; i++) {
// cout << i << ":";
// for (auto x : edges[i])
// cout << x << " ";
// cout << nl;
// }
int res = INT_MAX;
dfs(1, -1, tot, edges, vertex, res);
cout << res << nl;
}
anvnh {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fastio int ntest;
ntest = 1;
// cin >> ntest;
while (ntest--) {
clock_t z = clock();
solve();
debug("Total Time: %.7f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
}
return 0;
}