#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));
}
};
for (int i = 0; i < N; ++i) answer = std::max(answer, calcDp(i));
std::cout << answer << std::endl;
/*
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
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;*/
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 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 |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
21 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 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 |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
21 |
Correct |
1 ms |
304 KB |
Output is correct |
22 |
Correct |
110 ms |
444 KB |
Output is correct |
23 |
Correct |
115 ms |
444 KB |
Output is correct |
24 |
Correct |
111 ms |
340 KB |
Output is correct |
25 |
Correct |
119 ms |
340 KB |
Output is correct |
26 |
Correct |
105 ms |
444 KB |
Output is correct |
27 |
Correct |
103 ms |
340 KB |
Output is correct |
28 |
Correct |
99 ms |
444 KB |
Output is correct |
29 |
Correct |
95 ms |
532 KB |
Output is correct |
30 |
Correct |
100 ms |
532 KB |
Output is correct |
31 |
Correct |
74 ms |
444 KB |
Output is correct |
32 |
Correct |
73 ms |
444 KB |
Output is correct |
33 |
Correct |
86 ms |
444 KB |
Output is correct |
34 |
Correct |
92 ms |
440 KB |
Output is correct |
35 |
Correct |
94 ms |
432 KB |
Output is correct |
36 |
Correct |
90 ms |
340 KB |
Output is correct |
37 |
Correct |
82 ms |
468 KB |
Output is correct |
38 |
Correct |
83 ms |
452 KB |
Output is correct |
39 |
Correct |
105 ms |
488 KB |
Output is correct |
40 |
Correct |
105 ms |
488 KB |
Output is correct |
41 |
Correct |
108 ms |
484 KB |
Output is correct |
42 |
Correct |
105 ms |
468 KB |
Output is correct |
43 |
Correct |
110 ms |
468 KB |
Output is correct |
44 |
Correct |
77 ms |
468 KB |
Output is correct |
45 |
Correct |
82 ms |
468 KB |
Output is correct |
46 |
Correct |
83 ms |
464 KB |
Output is correct |
47 |
Correct |
76 ms |
468 KB |
Output is correct |
48 |
Correct |
76 ms |
468 KB |
Output is correct |
49 |
Correct |
88 ms |
492 KB |
Output is correct |
50 |
Correct |
70 ms |
480 KB |
Output is correct |
51 |
Correct |
79 ms |
468 KB |
Output is correct |
52 |
Correct |
91 ms |
448 KB |
Output is correct |
53 |
Correct |
84 ms |
468 KB |
Output is correct |
54 |
Correct |
81 ms |
480 KB |
Output is correct |
55 |
Correct |
87 ms |
468 KB |
Output is correct |
56 |
Correct |
59 ms |
468 KB |
Output is correct |
57 |
Correct |
98 ms |
468 KB |
Output is correct |
58 |
Correct |
66 ms |
468 KB |
Output is correct |
59 |
Correct |
67 ms |
472 KB |
Output is correct |
60 |
Correct |
109 ms |
468 KB |
Output is correct |
61 |
Correct |
62 ms |
468 KB |
Output is correct |
62 |
Correct |
108 ms |
532 KB |
Output is correct |
63 |
Correct |
68 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 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 |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
21 |
Correct |
1 ms |
304 KB |
Output is correct |
22 |
Correct |
110 ms |
444 KB |
Output is correct |
23 |
Correct |
115 ms |
444 KB |
Output is correct |
24 |
Correct |
111 ms |
340 KB |
Output is correct |
25 |
Correct |
119 ms |
340 KB |
Output is correct |
26 |
Correct |
105 ms |
444 KB |
Output is correct |
27 |
Correct |
103 ms |
340 KB |
Output is correct |
28 |
Correct |
99 ms |
444 KB |
Output is correct |
29 |
Correct |
95 ms |
532 KB |
Output is correct |
30 |
Correct |
100 ms |
532 KB |
Output is correct |
31 |
Correct |
74 ms |
444 KB |
Output is correct |
32 |
Correct |
73 ms |
444 KB |
Output is correct |
33 |
Correct |
86 ms |
444 KB |
Output is correct |
34 |
Correct |
92 ms |
440 KB |
Output is correct |
35 |
Correct |
94 ms |
432 KB |
Output is correct |
36 |
Correct |
90 ms |
340 KB |
Output is correct |
37 |
Correct |
82 ms |
468 KB |
Output is correct |
38 |
Correct |
83 ms |
452 KB |
Output is correct |
39 |
Correct |
105 ms |
488 KB |
Output is correct |
40 |
Correct |
105 ms |
488 KB |
Output is correct |
41 |
Correct |
108 ms |
484 KB |
Output is correct |
42 |
Correct |
105 ms |
468 KB |
Output is correct |
43 |
Correct |
110 ms |
468 KB |
Output is correct |
44 |
Correct |
77 ms |
468 KB |
Output is correct |
45 |
Correct |
82 ms |
468 KB |
Output is correct |
46 |
Correct |
83 ms |
464 KB |
Output is correct |
47 |
Correct |
76 ms |
468 KB |
Output is correct |
48 |
Correct |
76 ms |
468 KB |
Output is correct |
49 |
Correct |
88 ms |
492 KB |
Output is correct |
50 |
Correct |
70 ms |
480 KB |
Output is correct |
51 |
Correct |
79 ms |
468 KB |
Output is correct |
52 |
Correct |
91 ms |
448 KB |
Output is correct |
53 |
Correct |
84 ms |
468 KB |
Output is correct |
54 |
Correct |
81 ms |
480 KB |
Output is correct |
55 |
Correct |
87 ms |
468 KB |
Output is correct |
56 |
Correct |
59 ms |
468 KB |
Output is correct |
57 |
Correct |
98 ms |
468 KB |
Output is correct |
58 |
Correct |
66 ms |
468 KB |
Output is correct |
59 |
Correct |
67 ms |
472 KB |
Output is correct |
60 |
Correct |
109 ms |
468 KB |
Output is correct |
61 |
Correct |
62 ms |
468 KB |
Output is correct |
62 |
Correct |
108 ms |
532 KB |
Output is correct |
63 |
Correct |
68 ms |
340 KB |
Output is correct |
64 |
Execution timed out |
1567 ms |
17468 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |