#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n;
vector<vector<int>> graph;
vector<bool> removed;
vector<int> subtree_sz;
vector<bool> generator;
void calc_subtree_sz(int const cur, int const par) {
subtree_sz[cur] = 1;
for (int const& neigh : graph[cur]) {
if (neigh == par || removed[neigh])
continue;
calc_subtree_sz(neigh, cur);
subtree_sz[cur] += subtree_sz[neigh];
}
}
int find_centroid(int const cur, int const par, int const tree_sz) {
for (int const& neigh : graph[cur]) {
if (neigh == par || removed[neigh])
continue;
if (subtree_sz[neigh] > tree_sz / 2)
return find_centroid(neigh, cur, tree_sz);
}
return cur;
}
int calc_ans_rec(int const cur, int const par) {
int ans = -generator[cur];
for (int const neigh : graph[cur]) {
if (neigh == par || removed[neigh])
continue;
ans += calc_ans_rec(neigh, cur);
}
ans = max(ans, (int)generator[cur]);
return ans;
}
int calc_ans(int const centroid) {
int none = generator[centroid];
int one = 0;
int all = -generator[centroid];
for (int const neigh : graph[centroid]) {
if (removed[neigh])
continue;
int neigh_ans = calc_ans_rec(neigh, centroid);
one = max(one, neigh_ans + generator[centroid]);
all += neigh_ans;
}
return max({ none, one, all });
}
int decomp(int const cur) {
calc_subtree_sz(cur, -1);
int const centroid = find_centroid(cur, -1, subtree_sz[cur]);
int ans = calc_ans(centroid);
removed[centroid] = true;
for (int const neigh : graph[centroid]) {
if (removed[neigh])
continue;
ans = max(ans, decomp(neigh));
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
graph.resize(n);
removed.resize(n);
subtree_sz.resize(n);
generator.resize(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--; v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
string s;
cin >> s;
for (int i = 0; i < n; i++)
generator[i] = s[i] == '1';
int const ans = decomp(0);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
500 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
496 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
452 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
500 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
496 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
452 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
588 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
608 KB |
Output is correct |
46 |
Correct |
2 ms |
608 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
1 ms |
612 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
608 KB |
Output is correct |
51 |
Correct |
1 ms |
600 KB |
Output is correct |
52 |
Correct |
1 ms |
608 KB |
Output is correct |
53 |
Correct |
1 ms |
600 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
608 KB |
Output is correct |
59 |
Correct |
1 ms |
608 KB |
Output is correct |
60 |
Correct |
1 ms |
616 KB |
Output is correct |
61 |
Correct |
1 ms |
608 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
500 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
496 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
452 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
588 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
608 KB |
Output is correct |
46 |
Correct |
2 ms |
608 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
1 ms |
612 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
608 KB |
Output is correct |
51 |
Correct |
1 ms |
600 KB |
Output is correct |
52 |
Correct |
1 ms |
608 KB |
Output is correct |
53 |
Correct |
1 ms |
600 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
608 KB |
Output is correct |
59 |
Correct |
1 ms |
608 KB |
Output is correct |
60 |
Correct |
1 ms |
616 KB |
Output is correct |
61 |
Correct |
1 ms |
608 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
240 ms |
15392 KB |
Output is correct |
65 |
Correct |
201 ms |
15488 KB |
Output is correct |
66 |
Correct |
239 ms |
15360 KB |
Output is correct |
67 |
Correct |
219 ms |
15412 KB |
Output is correct |
68 |
Correct |
193 ms |
15348 KB |
Output is correct |
69 |
Correct |
179 ms |
15408 KB |
Output is correct |
70 |
Correct |
203 ms |
15416 KB |
Output is correct |
71 |
Correct |
171 ms |
15392 KB |
Output is correct |
72 |
Correct |
182 ms |
15412 KB |
Output is correct |
73 |
Correct |
394 ms |
25236 KB |
Output is correct |
74 |
Correct |
391 ms |
25212 KB |
Output is correct |
75 |
Correct |
182 ms |
15348 KB |
Output is correct |
76 |
Correct |
158 ms |
15264 KB |
Output is correct |
77 |
Correct |
195 ms |
15256 KB |
Output is correct |
78 |
Correct |
199 ms |
15240 KB |
Output is correct |
79 |
Correct |
202 ms |
15252 KB |
Output is correct |
80 |
Correct |
256 ms |
15252 KB |
Output is correct |
81 |
Correct |
76 ms |
15716 KB |
Output is correct |
82 |
Correct |
90 ms |
15480 KB |
Output is correct |
83 |
Correct |
301 ms |
19352 KB |
Output is correct |
84 |
Correct |
319 ms |
21152 KB |
Output is correct |
85 |
Correct |
256 ms |
20732 KB |
Output is correct |
86 |
Correct |
252 ms |
16256 KB |
Output is correct |
87 |
Correct |
248 ms |
16016 KB |
Output is correct |
88 |
Correct |
173 ms |
16460 KB |
Output is correct |
89 |
Correct |
181 ms |
16432 KB |
Output is correct |
90 |
Correct |
165 ms |
16372 KB |
Output is correct |
91 |
Correct |
238 ms |
16504 KB |
Output is correct |
92 |
Correct |
173 ms |
16624 KB |
Output is correct |
93 |
Correct |
220 ms |
20716 KB |
Output is correct |
94 |
Correct |
213 ms |
22316 KB |
Output is correct |
95 |
Correct |
181 ms |
19084 KB |
Output is correct |
96 |
Correct |
200 ms |
17656 KB |
Output is correct |
97 |
Correct |
192 ms |
16320 KB |
Output is correct |
98 |
Correct |
207 ms |
15916 KB |
Output is correct |
99 |
Correct |
149 ms |
15668 KB |
Output is correct |
100 |
Correct |
207 ms |
18072 KB |
Output is correct |
101 |
Correct |
218 ms |
16660 KB |
Output is correct |
102 |
Correct |
184 ms |
15988 KB |
Output is correct |
103 |
Correct |
234 ms |
15940 KB |
Output is correct |
104 |
Correct |
57 ms |
16044 KB |
Output is correct |
105 |
Correct |
429 ms |
22932 KB |
Output is correct |
106 |
Correct |
54 ms |
16072 KB |
Output is correct |
107 |
Correct |
51 ms |
15912 KB |
Output is correct |
108 |
Correct |
404 ms |
26236 KB |
Output is correct |
109 |
Correct |
51 ms |
16068 KB |
Output is correct |
110 |
Correct |
408 ms |
26224 KB |
Output is correct |
111 |
Correct |
140 ms |
15336 KB |
Output is correct |