#include <bits/stdc++.h>
int main() {
// centroid decomposition
int N;
std::cin >> N;
std::vector<int> A(N - 1), B(N - 1);
std::vector<std::vector<int>> Tree(N);
for (int i = 0; i < N - 1; ++i) {
std::cin >> A[i] >> B[i];
--A[i], --B[i];
Tree[A[i]].push_back(B[i]);
Tree[B[i]].push_back(A[i]);
}
std::string S;
std::cin >> S;
std::vector<bool> isValid(N, true);
std::vector<int> subtreeSize(N), dp(N);
int answer = 0;
auto calcDp = [&](const int root) {
auto dfs = [&](auto &&self, const int v, const int p) -> int {
dp[v] = (S[v] == '1' ? 1 : 0);
int sum = 0;
for (const int t : Tree[v]) {
if (t == p or (not isValid[t])) continue;
sum += self(self, t, v);
}
if (S[v] == '1') --sum;
dp[v] = std::max(dp[v], sum);
return dp[v];
};
std::vector<int> vals;
for (const int t : Tree[root]) {
if (not isValid[t]) continue;
vals.push_back(dfs(dfs, t, root));
}
std::sort(vals.begin(), vals.end(), std::greater());
while ((not vals.empty()) and vals.back() == 0) vals.pop_back();
if (vals.size() == 0) {
return S[root] == '1' ? 1 : 0;
} else {
const int v = std::accumulate(vals.begin(), vals.end(), 0);
return std::max(v - (S[root] == '1' ? 1 : 0), vals[0] + (S[root] == '1' ? 1 : 0));
}
};
auto centroidDecomposite = [&](auto &&self, const int root) -> void {
// calc subtree size
auto dfs1 = [&](auto &&self2, const int v, const int p) -> int {
subtreeSize[v] = 1;
for (const int t : Tree[v]) {
if (t == p or (not isValid[t])) continue;
subtreeSize[v] += self2(self2, t, v);
}
return subtreeSize[v];
};
dfs1(dfs1, root, -1);
// find centroid
const int mas = subtreeSize[root];
auto dfs2 = [&](auto &&self2, const int v, const int p) -> int {
bool isCentroid = true;
for (const int t : Tree[v]) {
if (t == p or (not isValid[t])) continue;
const int res = self2(self2, t, v);
if (res != -1) return res;
if (subtreeSize[t] > mas / 2) isCentroid = false;
}
if ((mas - subtreeSize[v]) > mas/ 2) isCentroid = false;
return isCentroid ? v : -1;
};
const int centroid = dfs2(dfs2, root, -1);
// dp
answer = std::max(answer, calcDp(centroid));
// rec
isValid[centroid] = false;
for (const int t : Tree[centroid]) {
if (isValid[t]) {
self(self, t);
}
}
};
centroidDecomposite(centroidDecomposite, 0);
std::cout << answer << std::endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
436 KB |
Output is correct |
24 |
Correct |
4 ms |
468 KB |
Output is correct |
25 |
Correct |
4 ms |
432 KB |
Output is correct |
26 |
Correct |
4 ms |
468 KB |
Output is correct |
27 |
Correct |
4 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
4 ms |
432 KB |
Output is correct |
30 |
Correct |
4 ms |
444 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
3 ms |
436 KB |
Output is correct |
33 |
Correct |
3 ms |
468 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
3 ms |
436 KB |
Output is correct |
36 |
Correct |
3 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
432 KB |
Output is correct |
38 |
Correct |
3 ms |
468 KB |
Output is correct |
39 |
Correct |
5 ms |
428 KB |
Output is correct |
40 |
Correct |
4 ms |
468 KB |
Output is correct |
41 |
Correct |
4 ms |
468 KB |
Output is correct |
42 |
Correct |
4 ms |
468 KB |
Output is correct |
43 |
Correct |
4 ms |
468 KB |
Output is correct |
44 |
Correct |
3 ms |
436 KB |
Output is correct |
45 |
Correct |
3 ms |
468 KB |
Output is correct |
46 |
Correct |
4 ms |
468 KB |
Output is correct |
47 |
Correct |
3 ms |
468 KB |
Output is correct |
48 |
Correct |
4 ms |
468 KB |
Output is correct |
49 |
Correct |
3 ms |
440 KB |
Output is correct |
50 |
Correct |
3 ms |
432 KB |
Output is correct |
51 |
Correct |
3 ms |
440 KB |
Output is correct |
52 |
Correct |
3 ms |
440 KB |
Output is correct |
53 |
Correct |
3 ms |
440 KB |
Output is correct |
54 |
Correct |
3 ms |
468 KB |
Output is correct |
55 |
Correct |
4 ms |
468 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
5 ms |
428 KB |
Output is correct |
58 |
Correct |
2 ms |
444 KB |
Output is correct |
59 |
Correct |
2 ms |
440 KB |
Output is correct |
60 |
Correct |
4 ms |
468 KB |
Output is correct |
61 |
Correct |
2 ms |
468 KB |
Output is correct |
62 |
Correct |
3 ms |
476 KB |
Output is correct |
63 |
Correct |
3 ms |
452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
436 KB |
Output is correct |
24 |
Correct |
4 ms |
468 KB |
Output is correct |
25 |
Correct |
4 ms |
432 KB |
Output is correct |
26 |
Correct |
4 ms |
468 KB |
Output is correct |
27 |
Correct |
4 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
4 ms |
432 KB |
Output is correct |
30 |
Correct |
4 ms |
444 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
3 ms |
436 KB |
Output is correct |
33 |
Correct |
3 ms |
468 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
3 ms |
436 KB |
Output is correct |
36 |
Correct |
3 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
432 KB |
Output is correct |
38 |
Correct |
3 ms |
468 KB |
Output is correct |
39 |
Correct |
5 ms |
428 KB |
Output is correct |
40 |
Correct |
4 ms |
468 KB |
Output is correct |
41 |
Correct |
4 ms |
468 KB |
Output is correct |
42 |
Correct |
4 ms |
468 KB |
Output is correct |
43 |
Correct |
4 ms |
468 KB |
Output is correct |
44 |
Correct |
3 ms |
436 KB |
Output is correct |
45 |
Correct |
3 ms |
468 KB |
Output is correct |
46 |
Correct |
4 ms |
468 KB |
Output is correct |
47 |
Correct |
3 ms |
468 KB |
Output is correct |
48 |
Correct |
4 ms |
468 KB |
Output is correct |
49 |
Correct |
3 ms |
440 KB |
Output is correct |
50 |
Correct |
3 ms |
432 KB |
Output is correct |
51 |
Correct |
3 ms |
440 KB |
Output is correct |
52 |
Correct |
3 ms |
440 KB |
Output is correct |
53 |
Correct |
3 ms |
440 KB |
Output is correct |
54 |
Correct |
3 ms |
468 KB |
Output is correct |
55 |
Correct |
4 ms |
468 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
5 ms |
428 KB |
Output is correct |
58 |
Correct |
2 ms |
444 KB |
Output is correct |
59 |
Correct |
2 ms |
440 KB |
Output is correct |
60 |
Correct |
4 ms |
468 KB |
Output is correct |
61 |
Correct |
2 ms |
468 KB |
Output is correct |
62 |
Correct |
3 ms |
476 KB |
Output is correct |
63 |
Correct |
3 ms |
452 KB |
Output is correct |
64 |
Correct |
738 ms |
17548 KB |
Output is correct |
65 |
Correct |
683 ms |
17548 KB |
Output is correct |
66 |
Correct |
729 ms |
17544 KB |
Output is correct |
67 |
Correct |
715 ms |
17540 KB |
Output is correct |
68 |
Correct |
668 ms |
17536 KB |
Output is correct |
69 |
Correct |
676 ms |
17436 KB |
Output is correct |
70 |
Correct |
695 ms |
17628 KB |
Output is correct |
71 |
Correct |
628 ms |
17544 KB |
Output is correct |
72 |
Correct |
682 ms |
17544 KB |
Output is correct |
73 |
Correct |
1035 ms |
23940 KB |
Output is correct |
74 |
Correct |
1010 ms |
23976 KB |
Output is correct |
75 |
Correct |
565 ms |
17372 KB |
Output is correct |
76 |
Correct |
716 ms |
17468 KB |
Output is correct |
77 |
Correct |
879 ms |
17440 KB |
Output is correct |
78 |
Correct |
679 ms |
17444 KB |
Output is correct |
79 |
Correct |
747 ms |
17568 KB |
Output is correct |
80 |
Correct |
727 ms |
17436 KB |
Output is correct |
81 |
Correct |
277 ms |
18628 KB |
Output is correct |
82 |
Correct |
285 ms |
17888 KB |
Output is correct |
83 |
Correct |
839 ms |
20128 KB |
Output is correct |
84 |
Correct |
824 ms |
21340 KB |
Output is correct |
85 |
Correct |
899 ms |
21052 KB |
Output is correct |
86 |
Correct |
747 ms |
18228 KB |
Output is correct |
87 |
Correct |
796 ms |
17984 KB |
Output is correct |
88 |
Correct |
581 ms |
18556 KB |
Output is correct |
89 |
Correct |
530 ms |
18528 KB |
Output is correct |
90 |
Correct |
621 ms |
18396 KB |
Output is correct |
91 |
Correct |
588 ms |
18400 KB |
Output is correct |
92 |
Correct |
516 ms |
18544 KB |
Output is correct |
93 |
Correct |
608 ms |
21208 KB |
Output is correct |
94 |
Correct |
578 ms |
22300 KB |
Output is correct |
95 |
Correct |
550 ms |
20152 KB |
Output is correct |
96 |
Correct |
620 ms |
19272 KB |
Output is correct |
97 |
Correct |
541 ms |
18192 KB |
Output is correct |
98 |
Correct |
462 ms |
18072 KB |
Output is correct |
99 |
Correct |
561 ms |
17888 KB |
Output is correct |
100 |
Correct |
542 ms |
19584 KB |
Output is correct |
101 |
Correct |
548 ms |
18472 KB |
Output is correct |
102 |
Correct |
546 ms |
18148 KB |
Output is correct |
103 |
Correct |
582 ms |
18140 KB |
Output is correct |
104 |
Correct |
189 ms |
20116 KB |
Output is correct |
105 |
Correct |
908 ms |
22420 KB |
Output is correct |
106 |
Correct |
205 ms |
19832 KB |
Output is correct |
107 |
Correct |
187 ms |
19888 KB |
Output is correct |
108 |
Correct |
1000 ms |
24324 KB |
Output is correct |
109 |
Correct |
206 ms |
20140 KB |
Output is correct |
110 |
Correct |
1018 ms |
24560 KB |
Output is correct |
111 |
Correct |
458 ms |
17460 KB |
Output is correct |