#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 1e9;
int n, dp[200001][2];
vector<int> adj[200001];
string s;
void DFS(int u, int p)
{
dp[u][0] = dp[u][1] = -INF;
int mx1 = -INF, mx2 = -INF;
vector<int> val;
for (auto v : adj[u])
{
if (v != p)
{
DFS(v, u);
mx1 = max(mx1, dp[v][0]);
mx2 = max(mx2, dp[v][1]);
val.push_back(dp[v][1]);
}
}
sort(val.begin(), val.end(), greater<int>());
int S = 0, i;
for (i = 0; i < val.size() && val[i] > 0; i++) {S += val[i];}
if (s[u] == '1') {dp[u][0] = dp[u][1] = 1;}
dp[u][0] = max(dp[u][0], mx1);
dp[u][0] = max(dp[u][0], mx2 + (s[u] == '1'));
if (i > 1) dp[u][0] = max(dp[u][0], S - (s[u] == '1'));
dp[u][1] = max(dp[u][1], mx2 - (s[u] == '1'));
if (i > 1) dp[u][1] = max(dp[u][1], S - (s[u] == '1'));
//cout << u << " " << dp[u][0] << " " << dp[u][1] << "\n";
}
int main()
{
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++)
{
int u, v; cin >> u >> v;
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
cin >> s;
DFS(0, -1);
cout << dp[0][0] << "\n";
return 0;
}
Compilation message
power.cpp: In function 'void DFS(int, int)':
power.cpp:29:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (i = 0; i < val.size() && val[i] > 0; i++) {S += val[i];}
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 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 |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 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 |
4968 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
5 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 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 |
5 ms |
4992 KB |
Output is correct |
19 |
Correct |
5 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 |
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 |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 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 |
4968 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
5 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 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 |
5 ms |
4992 KB |
Output is correct |
19 |
Correct |
5 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 |
7 ms |
5184 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 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 |
5 ms |
5248 KB |
Output is correct |
31 |
Correct |
4 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
6 ms |
5120 KB |
Output is correct |
34 |
Correct |
7 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
6 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5100 KB |
Output is correct |
39 |
Correct |
4 ms |
5248 KB |
Output is correct |
40 |
Correct |
5 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5240 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
6 ms |
5120 KB |
Output is correct |
44 |
Correct |
6 ms |
5120 KB |
Output is correct |
45 |
Correct |
5 ms |
5248 KB |
Output is correct |
46 |
Correct |
6 ms |
5120 KB |
Output is correct |
47 |
Correct |
6 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5248 KB |
Output is correct |
49 |
Correct |
6 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5120 KB |
Output is correct |
51 |
Correct |
5 ms |
5152 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
6 ms |
5120 KB |
Output is correct |
54 |
Correct |
6 ms |
5120 KB |
Output is correct |
55 |
Correct |
5 ms |
5120 KB |
Output is correct |
56 |
Correct |
5 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 |
5 ms |
5120 KB |
Output is correct |
60 |
Correct |
5 ms |
5248 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 |
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 |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 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 |
4968 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
5 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 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 |
5 ms |
4992 KB |
Output is correct |
19 |
Correct |
5 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 |
7 ms |
5184 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 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 |
5 ms |
5248 KB |
Output is correct |
31 |
Correct |
4 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
6 ms |
5120 KB |
Output is correct |
34 |
Correct |
7 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
6 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5100 KB |
Output is correct |
39 |
Correct |
4 ms |
5248 KB |
Output is correct |
40 |
Correct |
5 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5240 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
6 ms |
5120 KB |
Output is correct |
44 |
Correct |
6 ms |
5120 KB |
Output is correct |
45 |
Correct |
5 ms |
5248 KB |
Output is correct |
46 |
Correct |
6 ms |
5120 KB |
Output is correct |
47 |
Correct |
6 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5248 KB |
Output is correct |
49 |
Correct |
6 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5120 KB |
Output is correct |
51 |
Correct |
5 ms |
5152 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
6 ms |
5120 KB |
Output is correct |
54 |
Correct |
6 ms |
5120 KB |
Output is correct |
55 |
Correct |
5 ms |
5120 KB |
Output is correct |
56 |
Correct |
5 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 |
5 ms |
5120 KB |
Output is correct |
60 |
Correct |
5 ms |
5248 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 |
193 ms |
13376 KB |
Output is correct |
65 |
Correct |
204 ms |
13368 KB |
Output is correct |
66 |
Correct |
188 ms |
13348 KB |
Output is correct |
67 |
Correct |
192 ms |
13420 KB |
Output is correct |
68 |
Correct |
197 ms |
13348 KB |
Output is correct |
69 |
Correct |
209 ms |
13320 KB |
Output is correct |
70 |
Correct |
187 ms |
13252 KB |
Output is correct |
71 |
Correct |
178 ms |
13252 KB |
Output is correct |
72 |
Correct |
196 ms |
13492 KB |
Output is correct |
73 |
Correct |
218 ms |
31032 KB |
Output is correct |
74 |
Correct |
228 ms |
30900 KB |
Output is correct |
75 |
Correct |
208 ms |
13240 KB |
Output is correct |
76 |
Correct |
192 ms |
13120 KB |
Output is correct |
77 |
Correct |
190 ms |
13236 KB |
Output is correct |
78 |
Correct |
204 ms |
13240 KB |
Output is correct |
79 |
Correct |
192 ms |
13124 KB |
Output is correct |
80 |
Correct |
192 ms |
13240 KB |
Output is correct |
81 |
Correct |
162 ms |
14452 KB |
Output is correct |
82 |
Correct |
189 ms |
13764 KB |
Output is correct |
83 |
Correct |
213 ms |
20844 KB |
Output is correct |
84 |
Correct |
226 ms |
24112 KB |
Output is correct |
85 |
Correct |
217 ms |
23608 KB |
Output is correct |
86 |
Correct |
191 ms |
15288 KB |
Output is correct |
87 |
Correct |
201 ms |
14408 KB |
Output is correct |
88 |
Correct |
184 ms |
14708 KB |
Output is correct |
89 |
Correct |
197 ms |
14792 KB |
Output is correct |
90 |
Correct |
177 ms |
14548 KB |
Output is correct |
91 |
Correct |
202 ms |
14864 KB |
Output is correct |
92 |
Correct |
196 ms |
15400 KB |
Output is correct |
93 |
Correct |
179 ms |
19076 KB |
Output is correct |
94 |
Correct |
189 ms |
21772 KB |
Output is correct |
95 |
Correct |
185 ms |
19480 KB |
Output is correct |
96 |
Correct |
179 ms |
16928 KB |
Output is correct |
97 |
Correct |
192 ms |
14720 KB |
Output is correct |
98 |
Correct |
171 ms |
13928 KB |
Output is correct |
99 |
Correct |
186 ms |
13684 KB |
Output is correct |
100 |
Correct |
197 ms |
18212 KB |
Output is correct |
101 |
Correct |
186 ms |
15412 KB |
Output is correct |
102 |
Correct |
168 ms |
14212 KB |
Output is correct |
103 |
Correct |
178 ms |
13912 KB |
Output is correct |
104 |
Correct |
116 ms |
15748 KB |
Output is correct |
105 |
Correct |
219 ms |
26552 KB |
Output is correct |
106 |
Correct |
123 ms |
15724 KB |
Output is correct |
107 |
Correct |
117 ms |
15724 KB |
Output is correct |
108 |
Correct |
213 ms |
32088 KB |
Output is correct |
109 |
Correct |
116 ms |
15724 KB |
Output is correct |
110 |
Correct |
221 ms |
32564 KB |
Output is correct |
111 |
Correct |
191 ms |
13128 KB |
Output is correct |