-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagc034e.cpp
More file actions
40 lines (40 loc) · 940 Bytes
/
Copy pathagc034e.cpp
File metadata and controls
40 lines (40 loc) · 940 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
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 2020;
int n, rt, cnt[N], sum[N], son[N], f[N];
vector<int> G[N];
char s[N];
void dfs(int u, int d = 0, int fa = 0) {
son[u] = 0; cnt[u] = s[u] == '1'; sum[u] = 0; f[u] = 0;
for(int i = 0; i < (int) G[u].size(); i ++) {
int v = G[u][i];
if(v != fa) {
dfs(v, d + 1, u);
sum[u] += (sum[v] += cnt[v]);
cnt[u] += cnt[v];
if(sum[v] > sum[son[u]]) son[u] = v;
}
}
if(sum[son[u]] <= sum[u] - sum[son[u]]) {
f[u] = sum[u] / 2;
} else {
f[u] = sum[u] - sum[son[u]] + min(f[son[u]], (2 * sum[son[u]] - sum[u]) / 2);
}
}
int main() {
scanf("%d%s", &n, s + 1);
for(int x, y, i = 1; i < n; i ++) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
int ans = -1;
for(rt = 1; rt <= n; rt ++) {
dfs(rt);
if(f[rt] * 2 == sum[rt]) ans = ans == -1 ? f[rt] : min(ans, f[rt]);
}
printf("%d\n", ans);
return 0;
}