#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <algorithm>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 200000;
std::vector<int> g[1 + nmax];
char active[1 + nmax];
int dp[1 + nmax];
void dfs(int node, int parent) {
for(int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
if(to == parent)
g[node].erase(g[node].begin() + h);
}
dp[node] = -(active[node] - '0');
for(int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
dfs(to, node);
dp[node] += dp[to];
}
dp[node] = std::max(dp[node], active[node] - '0');
}
int solve(int node) {
int result = dp[node];
for(int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
result = std::max(result, solve(to));
result = std::max(result, dp[to] + active[node] - '0');
}
return result;
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int n;
std::cin >> n;
for(int i = 1; i < n; i++) {
int a, b;
std::cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
std::cin >> (active + 1);
dfs(1, 0);
std::cout << solve(1);
return 0;
}
Compilation message
power.cpp: In function 'void dfs(int, int)':
power.cpp:17:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int h = 0; h < g[node].size(); h++) {
| ~~^~~~~~~~~~~~~~~~
power.cpp:25:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int h = 0; h < g[node].size(); h++) {
| ~~^~~~~~~~~~~~~~~~
power.cpp: In function 'int solve(int)':
power.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int h = 0; h < g[node].size(); h++) {
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 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 |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 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 |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5120 KB |
Output is correct |
31 |
Correct |
6 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
4 ms |
5248 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5120 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
5 ms |
5120 KB |
Output is correct |
42 |
Correct |
4 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
5 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 |
4 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5120 KB |
Output is correct |
49 |
Correct |
4 ms |
5120 KB |
Output is correct |
50 |
Correct |
4 ms |
5120 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5120 KB |
Output is correct |
54 |
Correct |
4 ms |
5120 KB |
Output is correct |
55 |
Correct |
4 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 |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5120 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
5 ms |
5248 KB |
Output is correct |
63 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 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 |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5120 KB |
Output is correct |
31 |
Correct |
6 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
4 ms |
5248 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5120 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
5 ms |
5120 KB |
Output is correct |
42 |
Correct |
4 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
5 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 |
4 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5120 KB |
Output is correct |
49 |
Correct |
4 ms |
5120 KB |
Output is correct |
50 |
Correct |
4 ms |
5120 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5120 KB |
Output is correct |
54 |
Correct |
4 ms |
5120 KB |
Output is correct |
55 |
Correct |
4 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 |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5120 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
5 ms |
5248 KB |
Output is correct |
63 |
Correct |
4 ms |
5120 KB |
Output is correct |
64 |
Correct |
199 ms |
12408 KB |
Output is correct |
65 |
Correct |
199 ms |
12408 KB |
Output is correct |
66 |
Correct |
194 ms |
12408 KB |
Output is correct |
67 |
Correct |
219 ms |
12408 KB |
Output is correct |
68 |
Correct |
192 ms |
12392 KB |
Output is correct |
69 |
Correct |
202 ms |
12472 KB |
Output is correct |
70 |
Correct |
199 ms |
12536 KB |
Output is correct |
71 |
Correct |
193 ms |
12408 KB |
Output is correct |
72 |
Correct |
201 ms |
12408 KB |
Output is correct |
73 |
Correct |
234 ms |
22524 KB |
Output is correct |
74 |
Correct |
226 ms |
22392 KB |
Output is correct |
75 |
Correct |
185 ms |
12248 KB |
Output is correct |
76 |
Correct |
192 ms |
12408 KB |
Output is correct |
77 |
Correct |
202 ms |
12280 KB |
Output is correct |
78 |
Correct |
201 ms |
12280 KB |
Output is correct |
79 |
Correct |
207 ms |
12408 KB |
Output is correct |
80 |
Correct |
196 ms |
12280 KB |
Output is correct |
81 |
Correct |
159 ms |
12660 KB |
Output is correct |
82 |
Correct |
182 ms |
12532 KB |
Output is correct |
83 |
Correct |
223 ms |
16504 KB |
Output is correct |
84 |
Correct |
238 ms |
18168 KB |
Output is correct |
85 |
Correct |
245 ms |
17912 KB |
Output is correct |
86 |
Correct |
201 ms |
13508 KB |
Output is correct |
87 |
Correct |
219 ms |
12920 KB |
Output is correct |
88 |
Correct |
194 ms |
13432 KB |
Output is correct |
89 |
Correct |
186 ms |
13432 KB |
Output is correct |
90 |
Correct |
182 ms |
13304 KB |
Output is correct |
91 |
Correct |
204 ms |
13564 KB |
Output is correct |
92 |
Correct |
174 ms |
13756 KB |
Output is correct |
93 |
Correct |
196 ms |
15680 KB |
Output is correct |
94 |
Correct |
174 ms |
17008 KB |
Output is correct |
95 |
Correct |
183 ms |
15988 KB |
Output is correct |
96 |
Correct |
175 ms |
14456 KB |
Output is correct |
97 |
Correct |
167 ms |
13304 KB |
Output is correct |
98 |
Correct |
168 ms |
12924 KB |
Output is correct |
99 |
Correct |
196 ms |
12664 KB |
Output is correct |
100 |
Correct |
187 ms |
15224 KB |
Output is correct |
101 |
Correct |
196 ms |
13776 KB |
Output is correct |
102 |
Correct |
172 ms |
13048 KB |
Output is correct |
103 |
Correct |
186 ms |
12920 KB |
Output is correct |
104 |
Correct |
106 ms |
13036 KB |
Output is correct |
105 |
Correct |
225 ms |
19832 KB |
Output is correct |
106 |
Correct |
104 ms |
13036 KB |
Output is correct |
107 |
Correct |
104 ms |
13084 KB |
Output is correct |
108 |
Correct |
229 ms |
22996 KB |
Output is correct |
109 |
Correct |
101 ms |
13032 KB |
Output is correct |
110 |
Correct |
228 ms |
23416 KB |
Output is correct |
111 |
Correct |
174 ms |
12280 KB |
Output is correct |