#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int>> adj(n);
for (auto i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
vector<bool> s(n);
auto ans = 0;
for (auto i = 0; i < n; ++i) {
char c;
cin >> c;
s[i] = (c == '1');
ans += int(s[i]);
}
ans = min(ans, 2);
vector<int> dp(n), sum(n);
auto f = [&](auto self, int u, int p) -> int {
for (auto v : adj[u]) {
if (v != p) {
sum[u] += self(self, v, u);
}
}
dp[u] = (s[u] ? max(sum[u] - 1, 1) : sum[u]);
return dp[u];
};
f(f, 0, 0);
vector<int> dp_reroot(n), sum_reroot(n);
auto f_reroot = [&](auto self, int u, int p) -> void {
if (u == p) {
dp_reroot[u] = dp[u];
sum_reroot[u] = sum[u];
} else {
sum_reroot[u] = sum[u] + (s[p] ? max(sum_reroot[p] - sum[u] - 1, 1)
: sum_reroot[p] - sum[u]);
dp_reroot[u] = (s[u] ? max(sum_reroot[u] - 1, 1) : sum_reroot[u]);
}
for (auto v : adj[u]) {
if (v != p) {
self(self, v, u);
}
}
};
f_reroot(f_reroot, 0, 0);
ans = max(ans, *max_element(begin(dp_reroot), end(dp_reroot)));
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
456 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
428 KB |
Output is correct |
29 |
Correct |
2 ms |
556 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
2 ms |
456 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
460 KB |
Output is correct |
45 |
Correct |
1 ms |
464 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
2 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
464 KB |
Output is correct |
49 |
Correct |
1 ms |
460 KB |
Output is correct |
50 |
Correct |
2 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
1 ms |
464 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
584 KB |
Output is correct |
58 |
Correct |
1 ms |
460 KB |
Output is correct |
59 |
Correct |
1 ms |
460 KB |
Output is correct |
60 |
Correct |
1 ms |
556 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
596 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
456 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
428 KB |
Output is correct |
29 |
Correct |
2 ms |
556 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
2 ms |
456 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
460 KB |
Output is correct |
45 |
Correct |
1 ms |
464 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
2 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
464 KB |
Output is correct |
49 |
Correct |
1 ms |
460 KB |
Output is correct |
50 |
Correct |
2 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
1 ms |
464 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
584 KB |
Output is correct |
58 |
Correct |
1 ms |
460 KB |
Output is correct |
59 |
Correct |
1 ms |
460 KB |
Output is correct |
60 |
Correct |
1 ms |
556 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
596 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
64 |
Correct |
170 ms |
15036 KB |
Output is correct |
65 |
Correct |
171 ms |
17180 KB |
Output is correct |
66 |
Correct |
145 ms |
17264 KB |
Output is correct |
67 |
Correct |
150 ms |
17228 KB |
Output is correct |
68 |
Correct |
152 ms |
17280 KB |
Output is correct |
69 |
Correct |
135 ms |
17264 KB |
Output is correct |
70 |
Correct |
198 ms |
17276 KB |
Output is correct |
71 |
Correct |
141 ms |
17356 KB |
Output is correct |
72 |
Correct |
168 ms |
17228 KB |
Output is correct |
73 |
Correct |
191 ms |
32428 KB |
Output is correct |
74 |
Correct |
167 ms |
32256 KB |
Output is correct |
75 |
Correct |
136 ms |
17108 KB |
Output is correct |
76 |
Correct |
142 ms |
17140 KB |
Output is correct |
77 |
Correct |
162 ms |
17100 KB |
Output is correct |
78 |
Correct |
150 ms |
17360 KB |
Output is correct |
79 |
Correct |
170 ms |
17096 KB |
Output is correct |
80 |
Correct |
151 ms |
17096 KB |
Output is correct |
81 |
Correct |
127 ms |
17604 KB |
Output is correct |
82 |
Correct |
138 ms |
17368 KB |
Output is correct |
83 |
Correct |
174 ms |
23336 KB |
Output is correct |
84 |
Correct |
159 ms |
25932 KB |
Output is correct |
85 |
Correct |
161 ms |
25532 KB |
Output is correct |
86 |
Correct |
141 ms |
18896 KB |
Output is correct |
87 |
Correct |
165 ms |
18076 KB |
Output is correct |
88 |
Correct |
148 ms |
18512 KB |
Output is correct |
89 |
Correct |
136 ms |
18528 KB |
Output is correct |
90 |
Correct |
126 ms |
18384 KB |
Output is correct |
91 |
Correct |
145 ms |
18616 KB |
Output is correct |
92 |
Correct |
152 ms |
19032 KB |
Output is correct |
93 |
Correct |
125 ms |
21880 KB |
Output is correct |
94 |
Correct |
133 ms |
23868 KB |
Output is correct |
95 |
Correct |
121 ms |
22344 KB |
Output is correct |
96 |
Correct |
160 ms |
20328 KB |
Output is correct |
97 |
Correct |
128 ms |
18360 KB |
Output is correct |
98 |
Correct |
127 ms |
17768 KB |
Output is correct |
99 |
Correct |
138 ms |
17620 KB |
Output is correct |
100 |
Correct |
146 ms |
21432 KB |
Output is correct |
101 |
Correct |
150 ms |
18992 KB |
Output is correct |
102 |
Correct |
131 ms |
18100 KB |
Output is correct |
103 |
Correct |
144 ms |
17748 KB |
Output is correct |
104 |
Correct |
88 ms |
18060 KB |
Output is correct |
105 |
Correct |
173 ms |
28528 KB |
Output is correct |
106 |
Correct |
97 ms |
17844 KB |
Output is correct |
107 |
Correct |
75 ms |
17768 KB |
Output is correct |
108 |
Correct |
159 ms |
33244 KB |
Output is correct |
109 |
Correct |
75 ms |
18036 KB |
Output is correct |
110 |
Correct |
165 ms |
33672 KB |
Output is correct |
111 |
Correct |
155 ms |
17156 KB |
Output is correct |