#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+5;
vector<int> g[MAXN];
char special[MAXN];
int subsum[MAXN], dp[MAXN];
int n, ans;
void predfs(int u, int p) {
for (auto v : g[u]) {
if (v == p) continue;
predfs(v, u);
subsum[u] += dp[v];
}
dp[u] = subsum[u];
if (special[u] == '1') dp[u] = max(dp[u]-1, 1);
// printf("idp %d %d\n", u, dp[u]);
}
void dfs(int u, int p) {
if (special[u] == '1') {
int cnt = 0;
for (auto v : g[u]) cnt += dp[v] != 0;
if (cnt <= 1) ans = max(subsum[u]+1, ans);
else ans = max(dp[u], ans);
}
else {
ans = max(ans, dp[u]);
}
// printf("dp %d %d %d %d\n", u, dp[u], subsum[u], ans);
for (auto v : g[u]) {
if (v == p) continue;
int subu = subsum[u];
int subv = subsum[v];
int dpu = dp[u];
int dpv = dp[v];
subsum[u] -= dp[v];
dp[u] = subsum[u];
if (special[u] == '1') dp[u] = max(dp[u]-1, 1);
subsum[v] += dp[u];
dp[v] = subsum[v];
if (special[v] == '1') dp[v] = max(dp[v]-1, 1);
dfs(v, u);
subsum[u] = subu;
subsum[v] = subv;
dp[u] = dpu;
dp[v] = dpv;
}
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n-1; i++) {
int a, b;
scanf("%d %d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
scanf(" %s", &special[1]);
predfs(1, 1);
dfs(1, 1);
printf("%d\n", ans);
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
57 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
power.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
60 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
power.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
64 | scanf(" %s", &special[1]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
5084 KB |
Output is correct |
27 |
Correct |
5 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5120 KB |
Output is correct |
33 |
Correct |
5 ms |
5120 KB |
Output is correct |
34 |
Correct |
4 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
5120 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
5 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
6 ms |
5120 KB |
Output is correct |
40 |
Correct |
5 ms |
5120 KB |
Output is correct |
41 |
Correct |
5 ms |
5120 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
5 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
5120 KB |
Output is correct |
45 |
Correct |
5 ms |
5120 KB |
Output is correct |
46 |
Correct |
5 ms |
5120 KB |
Output is correct |
47 |
Correct |
5 ms |
5120 KB |
Output is correct |
48 |
Correct |
5 ms |
5248 KB |
Output is correct |
49 |
Correct |
5 ms |
5248 KB |
Output is correct |
50 |
Correct |
5 ms |
5120 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
5 ms |
5120 KB |
Output is correct |
53 |
Correct |
5 ms |
5120 KB |
Output is correct |
54 |
Correct |
5 ms |
5120 KB |
Output is correct |
55 |
Correct |
5 ms |
5120 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
5 ms |
5248 KB |
Output is correct |
58 |
Correct |
5 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5248 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
6 ms |
5248 KB |
Output is correct |
63 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
5084 KB |
Output is correct |
27 |
Correct |
5 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5120 KB |
Output is correct |
33 |
Correct |
5 ms |
5120 KB |
Output is correct |
34 |
Correct |
4 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
5120 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
5 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
6 ms |
5120 KB |
Output is correct |
40 |
Correct |
5 ms |
5120 KB |
Output is correct |
41 |
Correct |
5 ms |
5120 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
5 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
5120 KB |
Output is correct |
45 |
Correct |
5 ms |
5120 KB |
Output is correct |
46 |
Correct |
5 ms |
5120 KB |
Output is correct |
47 |
Correct |
5 ms |
5120 KB |
Output is correct |
48 |
Correct |
5 ms |
5248 KB |
Output is correct |
49 |
Correct |
5 ms |
5248 KB |
Output is correct |
50 |
Correct |
5 ms |
5120 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
5 ms |
5120 KB |
Output is correct |
53 |
Correct |
5 ms |
5120 KB |
Output is correct |
54 |
Correct |
5 ms |
5120 KB |
Output is correct |
55 |
Correct |
5 ms |
5120 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
5 ms |
5248 KB |
Output is correct |
58 |
Correct |
5 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5248 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
6 ms |
5248 KB |
Output is correct |
63 |
Correct |
5 ms |
5120 KB |
Output is correct |
64 |
Correct |
255 ms |
13688 KB |
Output is correct |
65 |
Correct |
242 ms |
13816 KB |
Output is correct |
66 |
Correct |
255 ms |
13816 KB |
Output is correct |
67 |
Correct |
250 ms |
13688 KB |
Output is correct |
68 |
Correct |
257 ms |
13688 KB |
Output is correct |
69 |
Correct |
234 ms |
13760 KB |
Output is correct |
70 |
Correct |
222 ms |
13688 KB |
Output is correct |
71 |
Correct |
233 ms |
13688 KB |
Output is correct |
72 |
Correct |
231 ms |
13820 KB |
Output is correct |
73 |
Correct |
254 ms |
26232 KB |
Output is correct |
74 |
Correct |
260 ms |
26232 KB |
Output is correct |
75 |
Correct |
228 ms |
13560 KB |
Output is correct |
76 |
Correct |
229 ms |
13688 KB |
Output is correct |
77 |
Correct |
247 ms |
13572 KB |
Output is correct |
78 |
Correct |
244 ms |
13560 KB |
Output is correct |
79 |
Correct |
236 ms |
13688 KB |
Output is correct |
80 |
Correct |
235 ms |
13560 KB |
Output is correct |
81 |
Correct |
181 ms |
13936 KB |
Output is correct |
82 |
Correct |
226 ms |
13832 KB |
Output is correct |
83 |
Correct |
266 ms |
18804 KB |
Output is correct |
84 |
Correct |
237 ms |
20984 KB |
Output is correct |
85 |
Correct |
243 ms |
20632 KB |
Output is correct |
86 |
Correct |
242 ms |
14968 KB |
Output is correct |
87 |
Correct |
233 ms |
14364 KB |
Output is correct |
88 |
Correct |
252 ms |
14904 KB |
Output is correct |
89 |
Correct |
209 ms |
14840 KB |
Output is correct |
90 |
Correct |
226 ms |
14712 KB |
Output is correct |
91 |
Correct |
226 ms |
14840 KB |
Output is correct |
92 |
Correct |
210 ms |
15224 KB |
Output is correct |
93 |
Correct |
223 ms |
17676 KB |
Output is correct |
94 |
Correct |
217 ms |
19440 KB |
Output is correct |
95 |
Correct |
209 ms |
18036 KB |
Output is correct |
96 |
Correct |
218 ms |
16248 KB |
Output is correct |
97 |
Correct |
217 ms |
14584 KB |
Output is correct |
98 |
Correct |
210 ms |
14328 KB |
Output is correct |
99 |
Correct |
240 ms |
13816 KB |
Output is correct |
100 |
Correct |
215 ms |
17016 KB |
Output is correct |
101 |
Correct |
210 ms |
14968 KB |
Output is correct |
102 |
Correct |
212 ms |
14328 KB |
Output is correct |
103 |
Correct |
251 ms |
14072 KB |
Output is correct |
104 |
Correct |
126 ms |
14188 KB |
Output is correct |
105 |
Correct |
262 ms |
22904 KB |
Output is correct |
106 |
Correct |
135 ms |
14188 KB |
Output is correct |
107 |
Correct |
134 ms |
14160 KB |
Output is correct |
108 |
Correct |
272 ms |
26872 KB |
Output is correct |
109 |
Correct |
122 ms |
14184 KB |
Output is correct |
110 |
Correct |
273 ms |
27280 KB |
Output is correct |
111 |
Correct |
208 ms |
13560 KB |
Output is correct |