#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
int n;
char s[N];
int dp[N], sum[N], f[N], mx[N];
vector<int> g[N];
void dfs(int u, int p) {
for (int v : g[u]) {
if (v != p) {
dfs(v, u);
mx[u] = max(mx[u], dp[v]);
sum[u] += dp[v];
}
}
if (s[u] == '1') {
dp[u] = max(1, sum[u] - 1);
} else {
dp[u] = sum[u];
}
}
int res = 0;
void reroot(int u, int p) {
if (s[u] == '1') {
res = max({res, f[u] + sum[u] - 1, max(f[u], mx[u]) + 1});
}
for (int v : g[u]) {
if (v != p) {
if (s[u] == '1') {
f[v] = max(1, sum[u] - dp[v] + f[u] - 1);
} else {
f[v] = sum[u] - dp[v] + f[u];
}
reroot(v, u);
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
cin >> s[i];
}
dfs(1, 1);
reroot(1, 1);
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
5044 KB |
Output is correct |
6 |
Correct |
2 ms |
5164 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4960 KB |
Output is correct |
20 |
Correct |
2 ms |
4960 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
5044 KB |
Output is correct |
6 |
Correct |
2 ms |
5164 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4960 KB |
Output is correct |
20 |
Correct |
2 ms |
4960 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5208 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
4 ms |
5176 KB |
Output is correct |
26 |
Correct |
3 ms |
5208 KB |
Output is correct |
27 |
Correct |
4 ms |
5176 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
3 ms |
5288 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5212 KB |
Output is correct |
34 |
Correct |
2 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5176 KB |
Output is correct |
37 |
Correct |
2 ms |
5208 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5212 KB |
Output is correct |
41 |
Correct |
3 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
3 ms |
5176 KB |
Output is correct |
44 |
Correct |
4 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
4 ms |
5192 KB |
Output is correct |
47 |
Correct |
3 ms |
5212 KB |
Output is correct |
48 |
Correct |
3 ms |
5200 KB |
Output is correct |
49 |
Correct |
3 ms |
5380 KB |
Output is correct |
50 |
Correct |
2 ms |
5212 KB |
Output is correct |
51 |
Correct |
2 ms |
5212 KB |
Output is correct |
52 |
Correct |
3 ms |
5212 KB |
Output is correct |
53 |
Correct |
2 ms |
5212 KB |
Output is correct |
54 |
Correct |
3 ms |
5208 KB |
Output is correct |
55 |
Correct |
3 ms |
5180 KB |
Output is correct |
56 |
Correct |
2 ms |
5208 KB |
Output is correct |
57 |
Correct |
3 ms |
5208 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
3 ms |
5212 KB |
Output is correct |
60 |
Correct |
2 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
2 ms |
5212 KB |
Output is correct |
63 |
Correct |
4 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
5044 KB |
Output is correct |
6 |
Correct |
2 ms |
5164 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4960 KB |
Output is correct |
20 |
Correct |
2 ms |
4960 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5208 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
4 ms |
5176 KB |
Output is correct |
26 |
Correct |
3 ms |
5208 KB |
Output is correct |
27 |
Correct |
4 ms |
5176 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
3 ms |
5288 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5212 KB |
Output is correct |
34 |
Correct |
2 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5176 KB |
Output is correct |
37 |
Correct |
2 ms |
5208 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5212 KB |
Output is correct |
41 |
Correct |
3 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
3 ms |
5176 KB |
Output is correct |
44 |
Correct |
4 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
4 ms |
5192 KB |
Output is correct |
47 |
Correct |
3 ms |
5212 KB |
Output is correct |
48 |
Correct |
3 ms |
5200 KB |
Output is correct |
49 |
Correct |
3 ms |
5380 KB |
Output is correct |
50 |
Correct |
2 ms |
5212 KB |
Output is correct |
51 |
Correct |
2 ms |
5212 KB |
Output is correct |
52 |
Correct |
3 ms |
5212 KB |
Output is correct |
53 |
Correct |
2 ms |
5212 KB |
Output is correct |
54 |
Correct |
3 ms |
5208 KB |
Output is correct |
55 |
Correct |
3 ms |
5180 KB |
Output is correct |
56 |
Correct |
2 ms |
5208 KB |
Output is correct |
57 |
Correct |
3 ms |
5208 KB |
Output is correct |
58 |
Correct |
2 ms |
5212 KB |
Output is correct |
59 |
Correct |
3 ms |
5212 KB |
Output is correct |
60 |
Correct |
2 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
2 ms |
5212 KB |
Output is correct |
63 |
Correct |
4 ms |
5212 KB |
Output is correct |
64 |
Correct |
70 ms |
17492 KB |
Output is correct |
65 |
Correct |
72 ms |
17456 KB |
Output is correct |
66 |
Correct |
68 ms |
17452 KB |
Output is correct |
67 |
Correct |
82 ms |
17360 KB |
Output is correct |
68 |
Correct |
72 ms |
17492 KB |
Output is correct |
69 |
Correct |
66 ms |
17544 KB |
Output is correct |
70 |
Correct |
71 ms |
17492 KB |
Output is correct |
71 |
Correct |
72 ms |
17356 KB |
Output is correct |
72 |
Correct |
72 ms |
17504 KB |
Output is correct |
73 |
Correct |
94 ms |
29936 KB |
Output is correct |
74 |
Correct |
93 ms |
30040 KB |
Output is correct |
75 |
Correct |
63 ms |
17240 KB |
Output is correct |
76 |
Correct |
64 ms |
17232 KB |
Output is correct |
77 |
Correct |
78 ms |
17492 KB |
Output is correct |
78 |
Correct |
71 ms |
17220 KB |
Output is correct |
79 |
Correct |
69 ms |
17232 KB |
Output is correct |
80 |
Correct |
71 ms |
17232 KB |
Output is correct |
81 |
Correct |
58 ms |
17572 KB |
Output is correct |
82 |
Correct |
63 ms |
17616 KB |
Output is correct |
83 |
Correct |
75 ms |
22560 KB |
Output is correct |
84 |
Correct |
81 ms |
24736 KB |
Output is correct |
85 |
Correct |
102 ms |
24352 KB |
Output is correct |
86 |
Correct |
70 ms |
18768 KB |
Output is correct |
87 |
Correct |
68 ms |
18260 KB |
Output is correct |
88 |
Correct |
69 ms |
18672 KB |
Output is correct |
89 |
Correct |
71 ms |
18768 KB |
Output is correct |
90 |
Correct |
71 ms |
18512 KB |
Output is correct |
91 |
Correct |
69 ms |
18756 KB |
Output is correct |
92 |
Correct |
68 ms |
19024 KB |
Output is correct |
93 |
Correct |
72 ms |
21456 KB |
Output is correct |
94 |
Correct |
80 ms |
22948 KB |
Output is correct |
95 |
Correct |
68 ms |
21708 KB |
Output is correct |
96 |
Correct |
73 ms |
19984 KB |
Output is correct |
97 |
Correct |
64 ms |
18552 KB |
Output is correct |
98 |
Correct |
66 ms |
18000 KB |
Output is correct |
99 |
Correct |
68 ms |
17868 KB |
Output is correct |
100 |
Correct |
67 ms |
21072 KB |
Output is correct |
101 |
Correct |
77 ms |
19028 KB |
Output is correct |
102 |
Correct |
69 ms |
18256 KB |
Output is correct |
103 |
Correct |
73 ms |
18004 KB |
Output is correct |
104 |
Correct |
42 ms |
16708 KB |
Output is correct |
105 |
Correct |
97 ms |
26964 KB |
Output is correct |
106 |
Correct |
45 ms |
16512 KB |
Output is correct |
107 |
Correct |
49 ms |
16448 KB |
Output is correct |
108 |
Correct |
95 ms |
30804 KB |
Output is correct |
109 |
Correct |
54 ms |
16556 KB |
Output is correct |
110 |
Correct |
86 ms |
31032 KB |
Output is correct |
111 |
Correct |
66 ms |
17232 KB |
Output is correct |