#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
#include <chrono>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n;
cin >> n;
vector <vector <int>> g(n);
for (int i = 0; i + 1 < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
vector <int> c(n);
for (int i = 0; i < n; ++i) {
char r;
cin >> r;
c[i] = r - '0';
}
vector <vector <int>> dp(n, vector <int> (2));
function <void(int, int)> dfs = [&] (int u, int p) {
vector <int> gs;
for (auto v : g[u]) {
if (v != p) {
gs.push_back(v);
dfs(v, u);
}
}
if (c[u] == 0) {
for (auto v : gs) {
dp[u][1] += dp[v][1];
dp[u][0] = max(dp[u][0], dp[v][0]);
}
} else {
dp[u][0] = -1;
dp[u][1] = -1;
int mx = 0;
for (auto v : gs) {
dp[u][0] += dp[v][1];
dp[u][1] += dp[v][1];
mx = max(mx, dp[v][1]);
}
dp[u][0] = max(dp[u][0], 1 + mx);
dp[u][1] = max(dp[u][1], 1);
}
};
dfs(0, -1);
int ans = 0;
for (int i = 0; i < n; ++i) ans = max(ans, max(dp[i][0], dp[i][1]));
/*for (int i = 0; i < n; ++i) {
cout << dp[i][0] << ' ' << dp[i][1] << endl;
}*/
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
288 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
288 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
640 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
28 |
Correct |
2 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
768 KB |
Output is correct |
30 |
Correct |
2 ms |
768 KB |
Output is correct |
31 |
Correct |
2 ms |
768 KB |
Output is correct |
32 |
Correct |
2 ms |
616 KB |
Output is correct |
33 |
Correct |
2 ms |
640 KB |
Output is correct |
34 |
Correct |
2 ms |
640 KB |
Output is correct |
35 |
Correct |
2 ms |
640 KB |
Output is correct |
36 |
Correct |
2 ms |
640 KB |
Output is correct |
37 |
Correct |
1 ms |
640 KB |
Output is correct |
38 |
Correct |
2 ms |
640 KB |
Output is correct |
39 |
Correct |
2 ms |
640 KB |
Output is correct |
40 |
Correct |
2 ms |
640 KB |
Output is correct |
41 |
Correct |
2 ms |
768 KB |
Output is correct |
42 |
Correct |
2 ms |
640 KB |
Output is correct |
43 |
Correct |
2 ms |
640 KB |
Output is correct |
44 |
Correct |
2 ms |
640 KB |
Output is correct |
45 |
Correct |
2 ms |
640 KB |
Output is correct |
46 |
Correct |
2 ms |
672 KB |
Output is correct |
47 |
Correct |
2 ms |
640 KB |
Output is correct |
48 |
Correct |
2 ms |
768 KB |
Output is correct |
49 |
Correct |
2 ms |
768 KB |
Output is correct |
50 |
Correct |
2 ms |
640 KB |
Output is correct |
51 |
Correct |
2 ms |
640 KB |
Output is correct |
52 |
Correct |
2 ms |
640 KB |
Output is correct |
53 |
Correct |
2 ms |
640 KB |
Output is correct |
54 |
Correct |
2 ms |
640 KB |
Output is correct |
55 |
Correct |
2 ms |
640 KB |
Output is correct |
56 |
Correct |
1 ms |
640 KB |
Output is correct |
57 |
Correct |
2 ms |
896 KB |
Output is correct |
58 |
Correct |
1 ms |
640 KB |
Output is correct |
59 |
Correct |
1 ms |
640 KB |
Output is correct |
60 |
Correct |
2 ms |
768 KB |
Output is correct |
61 |
Correct |
1 ms |
640 KB |
Output is correct |
62 |
Correct |
2 ms |
768 KB |
Output is correct |
63 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
288 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
640 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
28 |
Correct |
2 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
768 KB |
Output is correct |
30 |
Correct |
2 ms |
768 KB |
Output is correct |
31 |
Correct |
2 ms |
768 KB |
Output is correct |
32 |
Correct |
2 ms |
616 KB |
Output is correct |
33 |
Correct |
2 ms |
640 KB |
Output is correct |
34 |
Correct |
2 ms |
640 KB |
Output is correct |
35 |
Correct |
2 ms |
640 KB |
Output is correct |
36 |
Correct |
2 ms |
640 KB |
Output is correct |
37 |
Correct |
1 ms |
640 KB |
Output is correct |
38 |
Correct |
2 ms |
640 KB |
Output is correct |
39 |
Correct |
2 ms |
640 KB |
Output is correct |
40 |
Correct |
2 ms |
640 KB |
Output is correct |
41 |
Correct |
2 ms |
768 KB |
Output is correct |
42 |
Correct |
2 ms |
640 KB |
Output is correct |
43 |
Correct |
2 ms |
640 KB |
Output is correct |
44 |
Correct |
2 ms |
640 KB |
Output is correct |
45 |
Correct |
2 ms |
640 KB |
Output is correct |
46 |
Correct |
2 ms |
672 KB |
Output is correct |
47 |
Correct |
2 ms |
640 KB |
Output is correct |
48 |
Correct |
2 ms |
768 KB |
Output is correct |
49 |
Correct |
2 ms |
768 KB |
Output is correct |
50 |
Correct |
2 ms |
640 KB |
Output is correct |
51 |
Correct |
2 ms |
640 KB |
Output is correct |
52 |
Correct |
2 ms |
640 KB |
Output is correct |
53 |
Correct |
2 ms |
640 KB |
Output is correct |
54 |
Correct |
2 ms |
640 KB |
Output is correct |
55 |
Correct |
2 ms |
640 KB |
Output is correct |
56 |
Correct |
1 ms |
640 KB |
Output is correct |
57 |
Correct |
2 ms |
896 KB |
Output is correct |
58 |
Correct |
1 ms |
640 KB |
Output is correct |
59 |
Correct |
1 ms |
640 KB |
Output is correct |
60 |
Correct |
2 ms |
768 KB |
Output is correct |
61 |
Correct |
1 ms |
640 KB |
Output is correct |
62 |
Correct |
2 ms |
768 KB |
Output is correct |
63 |
Correct |
1 ms |
512 KB |
Output is correct |
64 |
Correct |
245 ms |
25848 KB |
Output is correct |
65 |
Correct |
241 ms |
25848 KB |
Output is correct |
66 |
Correct |
238 ms |
25848 KB |
Output is correct |
67 |
Correct |
238 ms |
25848 KB |
Output is correct |
68 |
Correct |
228 ms |
25848 KB |
Output is correct |
69 |
Correct |
236 ms |
25848 KB |
Output is correct |
70 |
Correct |
242 ms |
25848 KB |
Output is correct |
71 |
Correct |
232 ms |
25976 KB |
Output is correct |
72 |
Correct |
237 ms |
25980 KB |
Output is correct |
73 |
Correct |
280 ms |
51268 KB |
Output is correct |
74 |
Correct |
273 ms |
51096 KB |
Output is correct |
75 |
Correct |
248 ms |
25720 KB |
Output is correct |
76 |
Correct |
248 ms |
25924 KB |
Output is correct |
77 |
Correct |
273 ms |
25848 KB |
Output is correct |
78 |
Correct |
242 ms |
25720 KB |
Output is correct |
79 |
Correct |
232 ms |
25700 KB |
Output is correct |
80 |
Correct |
244 ms |
25720 KB |
Output is correct |
81 |
Correct |
182 ms |
26992 KB |
Output is correct |
82 |
Correct |
213 ms |
26356 KB |
Output is correct |
83 |
Correct |
263 ms |
36344 KB |
Output is correct |
84 |
Correct |
261 ms |
40440 KB |
Output is correct |
85 |
Correct |
262 ms |
39836 KB |
Output is correct |
86 |
Correct |
240 ms |
28556 KB |
Output is correct |
87 |
Correct |
239 ms |
27476 KB |
Output is correct |
88 |
Correct |
229 ms |
27824 KB |
Output is correct |
89 |
Correct |
232 ms |
27764 KB |
Output is correct |
90 |
Correct |
219 ms |
27384 KB |
Output is correct |
91 |
Correct |
232 ms |
27940 KB |
Output is correct |
92 |
Correct |
230 ms |
28532 KB |
Output is correct |
93 |
Correct |
232 ms |
33752 KB |
Output is correct |
94 |
Correct |
237 ms |
36928 KB |
Output is correct |
95 |
Correct |
224 ms |
34676 KB |
Output is correct |
96 |
Correct |
235 ms |
30864 KB |
Output is correct |
97 |
Correct |
215 ms |
27612 KB |
Output is correct |
98 |
Correct |
222 ms |
26488 KB |
Output is correct |
99 |
Correct |
230 ms |
26232 KB |
Output is correct |
100 |
Correct |
243 ms |
32760 KB |
Output is correct |
101 |
Correct |
235 ms |
28612 KB |
Output is correct |
102 |
Correct |
223 ms |
27152 KB |
Output is correct |
103 |
Correct |
238 ms |
26488 KB |
Output is correct |
104 |
Correct |
125 ms |
28140 KB |
Output is correct |
105 |
Correct |
266 ms |
44792 KB |
Output is correct |
106 |
Correct |
165 ms |
28012 KB |
Output is correct |
107 |
Correct |
163 ms |
28012 KB |
Output is correct |
108 |
Correct |
276 ms |
52804 KB |
Output is correct |
109 |
Correct |
128 ms |
28172 KB |
Output is correct |
110 |
Correct |
275 ms |
53380 KB |
Output is correct |
111 |
Correct |
216 ms |
25720 KB |
Output is correct |