#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<int> adj[N];
int dp[N], rsm[N], sm[N];
string s;
void dfs(int u, int p = -1) {
dp[u] = -(s[u] == '1'); // don't cut
sm[u] = 0;
for (int v: adj[u]) if (v != p) {
dfs(v, u);
sm[u] += dp[v];
}
dp[u] = max(-dp[u], dp[u] + sm[u]);
}
void reroot(int u, int p = -1) {
if (~p) {
int vp = s[p] == '1' ? 1 : 0;
rsm[u] = sm[u] + max(vp, -vp + rsm[p] - dp[u]);
} else {
rsm[u] = sm[u];
}
int vu = s[u] == '1' ? 1 : 0;
dp[u] = max(vu, rsm[u] - vu);
for (int v: adj[u]) if (v != p) {
reroot(v, u);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v; u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> s;
int ans = 0;
for (int i = 0; i < n; i++)
ans += s[i] == '1';
ans = min(ans, 2);
dfs(0);
reroot(0);
for (int i = 0; i < n; i++)
ans = max(ans, dp[i]);
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
5012 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
3 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
5012 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
3 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
4 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5120 KB |
Output is correct |
33 |
Correct |
5 ms |
5120 KB |
Output is correct |
34 |
Correct |
4 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5248 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
5 ms |
5248 KB |
Output is correct |
45 |
Correct |
4 ms |
5120 KB |
Output is correct |
46 |
Correct |
4 ms |
5120 KB |
Output is correct |
47 |
Correct |
4 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5248 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5120 KB |
Output is correct |
54 |
Correct |
4 ms |
5120 KB |
Output is correct |
55 |
Correct |
4 ms |
5120 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
4 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5248 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
5 ms |
5236 KB |
Output is correct |
63 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
5012 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
3 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
4 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5120 KB |
Output is correct |
33 |
Correct |
5 ms |
5120 KB |
Output is correct |
34 |
Correct |
4 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5248 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
5 ms |
5248 KB |
Output is correct |
45 |
Correct |
4 ms |
5120 KB |
Output is correct |
46 |
Correct |
4 ms |
5120 KB |
Output is correct |
47 |
Correct |
4 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5248 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5120 KB |
Output is correct |
54 |
Correct |
4 ms |
5120 KB |
Output is correct |
55 |
Correct |
4 ms |
5120 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
4 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5248 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
5 ms |
5236 KB |
Output is correct |
63 |
Correct |
4 ms |
5120 KB |
Output is correct |
64 |
Correct |
202 ms |
14780 KB |
Output is correct |
65 |
Correct |
212 ms |
14748 KB |
Output is correct |
66 |
Correct |
214 ms |
14756 KB |
Output is correct |
67 |
Correct |
207 ms |
14780 KB |
Output is correct |
68 |
Correct |
202 ms |
14884 KB |
Output is correct |
69 |
Correct |
206 ms |
14772 KB |
Output is correct |
70 |
Correct |
207 ms |
14788 KB |
Output is correct |
71 |
Correct |
208 ms |
14660 KB |
Output is correct |
72 |
Correct |
207 ms |
14900 KB |
Output is correct |
73 |
Correct |
244 ms |
24888 KB |
Output is correct |
74 |
Correct |
240 ms |
24724 KB |
Output is correct |
75 |
Correct |
199 ms |
14648 KB |
Output is correct |
76 |
Correct |
207 ms |
14656 KB |
Output is correct |
77 |
Correct |
212 ms |
14620 KB |
Output is correct |
78 |
Correct |
221 ms |
14784 KB |
Output is correct |
79 |
Correct |
217 ms |
14656 KB |
Output is correct |
80 |
Correct |
229 ms |
14736 KB |
Output is correct |
81 |
Correct |
167 ms |
15220 KB |
Output is correct |
82 |
Correct |
188 ms |
14968 KB |
Output is correct |
83 |
Correct |
221 ms |
18748 KB |
Output is correct |
84 |
Correct |
226 ms |
20552 KB |
Output is correct |
85 |
Correct |
224 ms |
20292 KB |
Output is correct |
86 |
Correct |
206 ms |
15800 KB |
Output is correct |
87 |
Correct |
210 ms |
15540 KB |
Output is correct |
88 |
Correct |
194 ms |
15860 KB |
Output is correct |
89 |
Correct |
202 ms |
15816 KB |
Output is correct |
90 |
Correct |
198 ms |
15772 KB |
Output is correct |
91 |
Correct |
201 ms |
15900 KB |
Output is correct |
92 |
Correct |
195 ms |
16168 KB |
Output is correct |
93 |
Correct |
194 ms |
18088 KB |
Output is correct |
94 |
Correct |
185 ms |
19440 KB |
Output is correct |
95 |
Correct |
178 ms |
18288 KB |
Output is correct |
96 |
Correct |
194 ms |
16804 KB |
Output is correct |
97 |
Correct |
196 ms |
15636 KB |
Output is correct |
98 |
Correct |
187 ms |
15336 KB |
Output is correct |
99 |
Correct |
210 ms |
15028 KB |
Output is correct |
100 |
Correct |
194 ms |
17700 KB |
Output is correct |
101 |
Correct |
190 ms |
16052 KB |
Output is correct |
102 |
Correct |
192 ms |
15528 KB |
Output is correct |
103 |
Correct |
200 ms |
15320 KB |
Output is correct |
104 |
Correct |
109 ms |
15468 KB |
Output is correct |
105 |
Correct |
242 ms |
22652 KB |
Output is correct |
106 |
Correct |
110 ms |
15592 KB |
Output is correct |
107 |
Correct |
108 ms |
15596 KB |
Output is correct |
108 |
Correct |
243 ms |
25400 KB |
Output is correct |
109 |
Correct |
110 ms |
15468 KB |
Output is correct |
110 |
Correct |
244 ms |
25784 KB |
Output is correct |
111 |
Correct |
188 ms |
14664 KB |
Output is correct |