#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
string str;
cin >> str;
vector<bool> active(n);
for (int i = 0; i < n; ++i) {
active[i] = str[i] - '0';
}
if (active == vector(n, false)) {
cout << 0 << '\n';
return 0;
}
vector<int> dp(n);
function<void(int, int)> dfs = [&](int v, int p) {
for (int to : adj[v]) {
if (to != p) {
dfs(to, v);
dp[v] += dp[to];
}
}
dp[v] -= active[v];
if (active[v]) {
dp[v] = max(dp[v], 1);
}
dp[v] = max(dp[v], 0);
};
dfs(0, -1);
int ans = 1;
function<void(int, int, int)> gfs = [&](int v, int p, int up) {
int sum = 0;
for (int to : adj[v]) {
if (to != p) {
sum += dp[to];
}
}
ans = max(ans, sum + up - active[v]);
sum = sum + up - active[v];
for (int to : adj[v]) {
if (to != p) {
int k = max(sum - dp[to], int(active[v]));
ans = max(ans, dp[to] + k);
gfs(to, v, k);
}
}
};
gfs(0, -1, 0);
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
0 ms |
320 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
0 ms |
320 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
428 KB |
Output is correct |
29 |
Correct |
1 ms |
560 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
424 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
456 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
456 KB |
Output is correct |
42 |
Correct |
1 ms |
460 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 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 |
460 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 |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
456 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
1 ms |
596 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
588 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
0 ms |
320 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
428 KB |
Output is correct |
29 |
Correct |
1 ms |
560 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
424 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
456 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
456 KB |
Output is correct |
42 |
Correct |
1 ms |
460 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 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 |
460 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 |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
456 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
1 ms |
596 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
588 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
135 ms |
15232 KB |
Output is correct |
65 |
Correct |
131 ms |
15116 KB |
Output is correct |
66 |
Correct |
145 ms |
15220 KB |
Output is correct |
67 |
Correct |
155 ms |
15184 KB |
Output is correct |
68 |
Correct |
148 ms |
15184 KB |
Output is correct |
69 |
Correct |
132 ms |
15100 KB |
Output is correct |
70 |
Correct |
134 ms |
15100 KB |
Output is correct |
71 |
Correct |
138 ms |
15128 KB |
Output is correct |
72 |
Correct |
155 ms |
15232 KB |
Output is correct |
73 |
Correct |
185 ms |
35344 KB |
Output is correct |
74 |
Correct |
171 ms |
35368 KB |
Output is correct |
75 |
Correct |
129 ms |
14984 KB |
Output is correct |
76 |
Correct |
130 ms |
15072 KB |
Output is correct |
77 |
Correct |
148 ms |
15216 KB |
Output is correct |
78 |
Correct |
146 ms |
15104 KB |
Output is correct |
79 |
Correct |
151 ms |
14988 KB |
Output is correct |
80 |
Correct |
154 ms |
15004 KB |
Output is correct |
81 |
Correct |
113 ms |
15612 KB |
Output is correct |
82 |
Correct |
141 ms |
15180 KB |
Output is correct |
83 |
Correct |
146 ms |
23428 KB |
Output is correct |
84 |
Correct |
150 ms |
26816 KB |
Output is correct |
85 |
Correct |
161 ms |
26264 KB |
Output is correct |
86 |
Correct |
140 ms |
17332 KB |
Output is correct |
87 |
Correct |
138 ms |
16328 KB |
Output is correct |
88 |
Correct |
130 ms |
16760 KB |
Output is correct |
89 |
Correct |
133 ms |
16860 KB |
Output is correct |
90 |
Correct |
129 ms |
16532 KB |
Output is correct |
91 |
Correct |
136 ms |
16860 KB |
Output is correct |
92 |
Correct |
137 ms |
17320 KB |
Output is correct |
93 |
Correct |
122 ms |
21288 KB |
Output is correct |
94 |
Correct |
136 ms |
24096 KB |
Output is correct |
95 |
Correct |
134 ms |
21928 KB |
Output is correct |
96 |
Correct |
130 ms |
19024 KB |
Output is correct |
97 |
Correct |
128 ms |
16688 KB |
Output is correct |
98 |
Correct |
130 ms |
15804 KB |
Output is correct |
99 |
Correct |
129 ms |
15488 KB |
Output is correct |
100 |
Correct |
133 ms |
20512 KB |
Output is correct |
101 |
Correct |
124 ms |
17308 KB |
Output is correct |
102 |
Correct |
131 ms |
16092 KB |
Output is correct |
103 |
Correct |
128 ms |
15788 KB |
Output is correct |
104 |
Correct |
68 ms |
16064 KB |
Output is correct |
105 |
Correct |
186 ms |
30292 KB |
Output is correct |
106 |
Correct |
76 ms |
15932 KB |
Output is correct |
107 |
Correct |
83 ms |
15864 KB |
Output is correct |
108 |
Correct |
171 ms |
36564 KB |
Output is correct |
109 |
Correct |
72 ms |
16048 KB |
Output is correct |
110 |
Correct |
164 ms |
37188 KB |
Output is correct |
111 |
Correct |
121 ms |
15196 KB |
Output is correct |