#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5 + 7;
const int INF = 1e9 + 7;
int n, dp[N][2][3], nxtDp[2][3], cnt[N];
string s;
vector<int> adj[N];
void Dfs(int u, int p){
int state = s[u] - '0';
cnt[u] = (state == 0);
for(int v : adj[u]){
if (v == p)
continue;
Dfs(v, u);
cnt[u] += cnt[v];
}
if (cnt[u] == 0)
return;
dp[u][state ^ 1][0] = 1;
dp[u][state ^ 1][1] = 1;
dp[u][state ^ 1][2] = 1;
for(int v : adj[u]){
if (v == p || cnt[v] == 0)
continue;
memset(nxtDp, 0x3f, sizeof(nxtDp));
for(int s = 0; s < 2; s++){
nxtDp[s][0] = min(nxtDp[s][0], dp[u][s][1] + dp[v][1][1]);
nxtDp[s][0] = min(nxtDp[s][0], dp[u][s ^ 1][1] + dp[v][0][1] + 2);
nxtDp[s][0] = min(nxtDp[s][0], dp[u][s][2] + dp[v][0][0] + 1);
nxtDp[s][0] = min(nxtDp[s][0], dp[u][s ^ 1][2] + dp[v][1][0] + 3);
nxtDp[s][0] = min(nxtDp[s][0], dp[u][s ^ 1][0] + dp[v][1][2] + 1);
nxtDp[s][0] = min(nxtDp[s][0], dp[u][s][0] + dp[v][0][2] + 3);
nxtDp[s][1] = min(nxtDp[s][1], dp[u][s ^ 1][1] + dp[v][1][2] + 1);
nxtDp[s][1] = min(nxtDp[s][1], dp[u][s][1] + dp[v][0][2] + 3);
nxtDp[s][1] = min(nxtDp[s][1], dp[u][s][2] + dp[v][1][1]);
nxtDp[s][1] = min(nxtDp[s][1], dp[u][s ^ 1][2] + dp[v][0][1] + 2);
nxtDp[s][2] = min(nxtDp[s][2], dp[u][s ^ 1][2] + dp[v][1][2] + 1);
nxtDp[s][2] = min(nxtDp[s][2], dp[u][s][2] + dp[v][0][2] + 3);
}
for(int s = 0; s < 2; s++)
for(int i = 1; i >= 0; i--)
nxtDp[s][i] = min(nxtDp[s][i], nxtDp[s][i + 1]);
for(int s = 0; s < 2; s++)
for(int i = 0; i < 3; i++)
dp[u][s][i] = nxtDp[s][i];
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
s = '*' + s;
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(dp, 0x3f, sizeof(dp));
int root = 1;
while (s[root] == '1')
root++;
Dfs(root, -1);
cout << dp[root][1][0];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23784 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
13 ms |
23756 KB |
Output is correct |
7 |
Correct |
12 ms |
23756 KB |
Output is correct |
8 |
Correct |
12 ms |
23712 KB |
Output is correct |
9 |
Correct |
14 ms |
23716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
268 ms |
63872 KB |
Output is correct |
2 |
Correct |
400 ms |
86112 KB |
Output is correct |
3 |
Correct |
373 ms |
91724 KB |
Output is correct |
4 |
Correct |
347 ms |
66044 KB |
Output is correct |
5 |
Correct |
388 ms |
74608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
329 ms |
38040 KB |
Output is correct |
2 |
Correct |
384 ms |
48180 KB |
Output is correct |
3 |
Correct |
422 ms |
48820 KB |
Output is correct |
4 |
Correct |
307 ms |
44892 KB |
Output is correct |
5 |
Correct |
302 ms |
48416 KB |
Output is correct |
6 |
Correct |
271 ms |
45828 KB |
Output is correct |
7 |
Correct |
329 ms |
49104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23784 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
13 ms |
23756 KB |
Output is correct |
7 |
Correct |
12 ms |
23756 KB |
Output is correct |
8 |
Correct |
12 ms |
23712 KB |
Output is correct |
9 |
Correct |
14 ms |
23716 KB |
Output is correct |
10 |
Correct |
268 ms |
63872 KB |
Output is correct |
11 |
Correct |
400 ms |
86112 KB |
Output is correct |
12 |
Correct |
373 ms |
91724 KB |
Output is correct |
13 |
Correct |
347 ms |
66044 KB |
Output is correct |
14 |
Correct |
388 ms |
74608 KB |
Output is correct |
15 |
Correct |
329 ms |
38040 KB |
Output is correct |
16 |
Correct |
384 ms |
48180 KB |
Output is correct |
17 |
Correct |
422 ms |
48820 KB |
Output is correct |
18 |
Correct |
307 ms |
44892 KB |
Output is correct |
19 |
Correct |
302 ms |
48416 KB |
Output is correct |
20 |
Correct |
271 ms |
45828 KB |
Output is correct |
21 |
Correct |
329 ms |
49104 KB |
Output is correct |
22 |
Correct |
350 ms |
45516 KB |
Output is correct |
23 |
Correct |
417 ms |
46708 KB |
Output is correct |
24 |
Correct |
352 ms |
45868 KB |
Output is correct |
25 |
Correct |
343 ms |
44572 KB |
Output is correct |
26 |
Correct |
367 ms |
50184 KB |
Output is correct |
27 |
Correct |
317 ms |
49972 KB |
Output is correct |
28 |
Correct |
272 ms |
46528 KB |
Output is correct |
29 |
Correct |
356 ms |
48788 KB |
Output is correct |
30 |
Correct |
321 ms |
47208 KB |
Output is correct |