#include <bits/stdc++.h>
int n, ans = 0;
std::vector <std::vector <int>> g;
std::vector <int> mem;
std::string s;
void dfs1(int node, int par = -1) {
for (int x : g[node]) if (x != par) dfs1(x, node);
int r = s[node] == '1';
mem[node] = std::max(mem[node], r);
int sm = 0;
for (int x : g[node]) if (x != par) sm += mem[x];
mem[node] = std::max(mem[node], sm - r);
}
void dfs2(int node, int par = -1) {
ans = std::max(ans, mem[node]);
for (int x : g[node]) if (x != par) dfs2(x, node);
int r = s[node] == '1';
if (!r) {
int sm = 0;
for (int x : g[node]) if (x != par) sm += mem[x];
ans = std::max(ans, sm);
} else {
int sm = 0;
for (int x : g[node]) if (x != par) ans = std::max(ans, mem[x] + 1), sm += mem[x];
ans = std::max(ans, sm - 1);
}
}
int main() {
std::ios::sync_with_stdio(0); std::cin.tie(0);
std::cin >> n;
g.resize(n);
mem.resize(n);
for (int i = 0; i < n - 1; i++) {
int u, v; std::cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
std::cin >> s;
dfs1(0); dfs2(0);
std::cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
416 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
512 KB |
Output is correct |
34 |
Correct |
1 ms |
512 KB |
Output is correct |
35 |
Correct |
2 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
512 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
1 ms |
512 KB |
Output is correct |
41 |
Correct |
1 ms |
512 KB |
Output is correct |
42 |
Correct |
2 ms |
512 KB |
Output is correct |
43 |
Correct |
1 ms |
512 KB |
Output is correct |
44 |
Correct |
1 ms |
512 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
2 ms |
512 KB |
Output is correct |
47 |
Correct |
1 ms |
512 KB |
Output is correct |
48 |
Correct |
1 ms |
512 KB |
Output is correct |
49 |
Correct |
1 ms |
512 KB |
Output is correct |
50 |
Correct |
1 ms |
640 KB |
Output is correct |
51 |
Correct |
2 ms |
512 KB |
Output is correct |
52 |
Correct |
1 ms |
512 KB |
Output is correct |
53 |
Correct |
1 ms |
512 KB |
Output is correct |
54 |
Correct |
1 ms |
512 KB |
Output is correct |
55 |
Correct |
1 ms |
544 KB |
Output is correct |
56 |
Correct |
1 ms |
512 KB |
Output is correct |
57 |
Correct |
2 ms |
640 KB |
Output is correct |
58 |
Correct |
1 ms |
512 KB |
Output is correct |
59 |
Correct |
1 ms |
512 KB |
Output is correct |
60 |
Correct |
1 ms |
512 KB |
Output is correct |
61 |
Correct |
1 ms |
512 KB |
Output is correct |
62 |
Correct |
1 ms |
640 KB |
Output is correct |
63 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
416 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
512 KB |
Output is correct |
34 |
Correct |
1 ms |
512 KB |
Output is correct |
35 |
Correct |
2 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
512 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
1 ms |
512 KB |
Output is correct |
41 |
Correct |
1 ms |
512 KB |
Output is correct |
42 |
Correct |
2 ms |
512 KB |
Output is correct |
43 |
Correct |
1 ms |
512 KB |
Output is correct |
44 |
Correct |
1 ms |
512 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
2 ms |
512 KB |
Output is correct |
47 |
Correct |
1 ms |
512 KB |
Output is correct |
48 |
Correct |
1 ms |
512 KB |
Output is correct |
49 |
Correct |
1 ms |
512 KB |
Output is correct |
50 |
Correct |
1 ms |
640 KB |
Output is correct |
51 |
Correct |
2 ms |
512 KB |
Output is correct |
52 |
Correct |
1 ms |
512 KB |
Output is correct |
53 |
Correct |
1 ms |
512 KB |
Output is correct |
54 |
Correct |
1 ms |
512 KB |
Output is correct |
55 |
Correct |
1 ms |
544 KB |
Output is correct |
56 |
Correct |
1 ms |
512 KB |
Output is correct |
57 |
Correct |
2 ms |
640 KB |
Output is correct |
58 |
Correct |
1 ms |
512 KB |
Output is correct |
59 |
Correct |
1 ms |
512 KB |
Output is correct |
60 |
Correct |
1 ms |
512 KB |
Output is correct |
61 |
Correct |
1 ms |
512 KB |
Output is correct |
62 |
Correct |
1 ms |
640 KB |
Output is correct |
63 |
Correct |
1 ms |
512 KB |
Output is correct |
64 |
Correct |
207 ms |
15456 KB |
Output is correct |
65 |
Correct |
204 ms |
15324 KB |
Output is correct |
66 |
Correct |
208 ms |
15312 KB |
Output is correct |
67 |
Correct |
207 ms |
15324 KB |
Output is correct |
68 |
Correct |
206 ms |
15312 KB |
Output is correct |
69 |
Correct |
205 ms |
15320 KB |
Output is correct |
70 |
Correct |
199 ms |
15332 KB |
Output is correct |
71 |
Correct |
198 ms |
15328 KB |
Output is correct |
72 |
Correct |
204 ms |
15320 KB |
Output is correct |
73 |
Correct |
245 ms |
27840 KB |
Output is correct |
74 |
Correct |
250 ms |
27832 KB |
Output is correct |
75 |
Correct |
198 ms |
15196 KB |
Output is correct |
76 |
Correct |
194 ms |
15204 KB |
Output is correct |
77 |
Correct |
209 ms |
15192 KB |
Output is correct |
78 |
Correct |
220 ms |
15204 KB |
Output is correct |
79 |
Correct |
208 ms |
15204 KB |
Output is correct |
80 |
Correct |
216 ms |
15196 KB |
Output is correct |
81 |
Correct |
164 ms |
15604 KB |
Output is correct |
82 |
Correct |
186 ms |
15352 KB |
Output is correct |
83 |
Correct |
222 ms |
20284 KB |
Output is correct |
84 |
Correct |
222 ms |
22476 KB |
Output is correct |
85 |
Correct |
216 ms |
22084 KB |
Output is correct |
86 |
Correct |
211 ms |
16568 KB |
Output is correct |
87 |
Correct |
211 ms |
16112 KB |
Output is correct |
88 |
Correct |
190 ms |
16372 KB |
Output is correct |
89 |
Correct |
198 ms |
16584 KB |
Output is correct |
90 |
Correct |
185 ms |
16284 KB |
Output is correct |
91 |
Correct |
186 ms |
16616 KB |
Output is correct |
92 |
Correct |
191 ms |
16840 KB |
Output is correct |
93 |
Correct |
179 ms |
19204 KB |
Output is correct |
94 |
Correct |
186 ms |
20976 KB |
Output is correct |
95 |
Correct |
181 ms |
19568 KB |
Output is correct |
96 |
Correct |
185 ms |
17832 KB |
Output is correct |
97 |
Correct |
172 ms |
16404 KB |
Output is correct |
98 |
Correct |
173 ms |
15864 KB |
Output is correct |
99 |
Correct |
191 ms |
15704 KB |
Output is correct |
100 |
Correct |
182 ms |
18724 KB |
Output is correct |
101 |
Correct |
181 ms |
16692 KB |
Output is correct |
102 |
Correct |
187 ms |
16120 KB |
Output is correct |
103 |
Correct |
188 ms |
15852 KB |
Output is correct |
104 |
Correct |
104 ms |
16108 KB |
Output is correct |
105 |
Correct |
240 ms |
24760 KB |
Output is correct |
106 |
Correct |
103 ms |
16108 KB |
Output is correct |
107 |
Correct |
103 ms |
15980 KB |
Output is correct |
108 |
Correct |
238 ms |
28600 KB |
Output is correct |
109 |
Correct |
106 ms |
16108 KB |
Output is correct |
110 |
Correct |
237 ms |
28984 KB |
Output is correct |
111 |
Correct |
181 ms |
15204 KB |
Output is correct |