#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector <int> g[N];
int dp[N], n;
char a[N];
int dfs(int nod, int dad)
{
int st = a[nod] - '0', bst_son = 0, bst = 0;
for (auto x : g[nod])
{
if (x == dad)
continue;
bst = max(bst, dfs(x, nod));
dp[nod] += dp[x];
bst_son = max(bst_son, dp[x]);
}
dp[nod] = max(st, dp[nod] - st); ///a or b...
return max(bst, max(dp[nod], bst_son + st));
}
int main()
{
cin >> n;
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> a + 1;
cout << dfs(1, 0);
return 0;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:32:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | cin >> a + 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4996 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4996 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
6 ms |
5096 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
5 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5008 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
5 ms |
5008 KB |
Output is correct |
28 |
Correct |
5 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
6 ms |
5140 KB |
Output is correct |
31 |
Correct |
4 ms |
5008 KB |
Output is correct |
32 |
Correct |
4 ms |
5012 KB |
Output is correct |
33 |
Correct |
5 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
5 ms |
5008 KB |
Output is correct |
36 |
Correct |
5 ms |
5072 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5068 KB |
Output is correct |
39 |
Correct |
5 ms |
5068 KB |
Output is correct |
40 |
Correct |
4 ms |
5068 KB |
Output is correct |
41 |
Correct |
4 ms |
5132 KB |
Output is correct |
42 |
Correct |
4 ms |
5012 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
5 ms |
5140 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
4 ms |
5068 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
5 ms |
5068 KB |
Output is correct |
49 |
Correct |
4 ms |
5136 KB |
Output is correct |
50 |
Correct |
4 ms |
5068 KB |
Output is correct |
51 |
Correct |
4 ms |
5068 KB |
Output is correct |
52 |
Correct |
5 ms |
5004 KB |
Output is correct |
53 |
Correct |
5 ms |
5004 KB |
Output is correct |
54 |
Correct |
5 ms |
5004 KB |
Output is correct |
55 |
Correct |
5 ms |
5068 KB |
Output is correct |
56 |
Correct |
4 ms |
5068 KB |
Output is correct |
57 |
Correct |
5 ms |
5196 KB |
Output is correct |
58 |
Correct |
5 ms |
5068 KB |
Output is correct |
59 |
Correct |
5 ms |
4980 KB |
Output is correct |
60 |
Correct |
4 ms |
5068 KB |
Output is correct |
61 |
Correct |
5 ms |
5068 KB |
Output is correct |
62 |
Correct |
5 ms |
5196 KB |
Output is correct |
63 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4996 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
6 ms |
5096 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
5 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5008 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
5 ms |
5008 KB |
Output is correct |
28 |
Correct |
5 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
6 ms |
5140 KB |
Output is correct |
31 |
Correct |
4 ms |
5008 KB |
Output is correct |
32 |
Correct |
4 ms |
5012 KB |
Output is correct |
33 |
Correct |
5 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
5 ms |
5008 KB |
Output is correct |
36 |
Correct |
5 ms |
5072 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5068 KB |
Output is correct |
39 |
Correct |
5 ms |
5068 KB |
Output is correct |
40 |
Correct |
4 ms |
5068 KB |
Output is correct |
41 |
Correct |
4 ms |
5132 KB |
Output is correct |
42 |
Correct |
4 ms |
5012 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
5 ms |
5140 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
4 ms |
5068 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
5 ms |
5068 KB |
Output is correct |
49 |
Correct |
4 ms |
5136 KB |
Output is correct |
50 |
Correct |
4 ms |
5068 KB |
Output is correct |
51 |
Correct |
4 ms |
5068 KB |
Output is correct |
52 |
Correct |
5 ms |
5004 KB |
Output is correct |
53 |
Correct |
5 ms |
5004 KB |
Output is correct |
54 |
Correct |
5 ms |
5004 KB |
Output is correct |
55 |
Correct |
5 ms |
5068 KB |
Output is correct |
56 |
Correct |
4 ms |
5068 KB |
Output is correct |
57 |
Correct |
5 ms |
5196 KB |
Output is correct |
58 |
Correct |
5 ms |
5068 KB |
Output is correct |
59 |
Correct |
5 ms |
4980 KB |
Output is correct |
60 |
Correct |
4 ms |
5068 KB |
Output is correct |
61 |
Correct |
5 ms |
5068 KB |
Output is correct |
62 |
Correct |
5 ms |
5196 KB |
Output is correct |
63 |
Correct |
4 ms |
5068 KB |
Output is correct |
64 |
Correct |
253 ms |
15044 KB |
Output is correct |
65 |
Correct |
253 ms |
15060 KB |
Output is correct |
66 |
Correct |
238 ms |
14952 KB |
Output is correct |
67 |
Correct |
256 ms |
15044 KB |
Output is correct |
68 |
Correct |
249 ms |
15012 KB |
Output is correct |
69 |
Correct |
247 ms |
15104 KB |
Output is correct |
70 |
Correct |
238 ms |
15008 KB |
Output is correct |
71 |
Correct |
256 ms |
15028 KB |
Output is correct |
72 |
Correct |
261 ms |
15048 KB |
Output is correct |
73 |
Correct |
272 ms |
30164 KB |
Output is correct |
74 |
Correct |
292 ms |
30088 KB |
Output is correct |
75 |
Correct |
266 ms |
14852 KB |
Output is correct |
76 |
Correct |
263 ms |
14844 KB |
Output is correct |
77 |
Correct |
300 ms |
14884 KB |
Output is correct |
78 |
Correct |
246 ms |
14904 KB |
Output is correct |
79 |
Correct |
317 ms |
14916 KB |
Output is correct |
80 |
Correct |
287 ms |
14828 KB |
Output is correct |
81 |
Correct |
227 ms |
15204 KB |
Output is correct |
82 |
Correct |
229 ms |
15028 KB |
Output is correct |
83 |
Correct |
274 ms |
21280 KB |
Output is correct |
84 |
Correct |
256 ms |
23748 KB |
Output is correct |
85 |
Correct |
311 ms |
23312 KB |
Output is correct |
86 |
Correct |
259 ms |
16572 KB |
Output is correct |
87 |
Correct |
249 ms |
15960 KB |
Output is correct |
88 |
Correct |
318 ms |
16392 KB |
Output is correct |
89 |
Correct |
247 ms |
16472 KB |
Output is correct |
90 |
Correct |
246 ms |
16088 KB |
Output is correct |
91 |
Correct |
246 ms |
16500 KB |
Output is correct |
92 |
Correct |
246 ms |
16904 KB |
Output is correct |
93 |
Correct |
285 ms |
19652 KB |
Output is correct |
94 |
Correct |
224 ms |
21672 KB |
Output is correct |
95 |
Correct |
244 ms |
20292 KB |
Output is correct |
96 |
Correct |
252 ms |
17984 KB |
Output is correct |
97 |
Correct |
246 ms |
16132 KB |
Output is correct |
98 |
Correct |
233 ms |
15692 KB |
Output is correct |
99 |
Correct |
284 ms |
15360 KB |
Output is correct |
100 |
Correct |
266 ms |
19216 KB |
Output is correct |
101 |
Correct |
256 ms |
16836 KB |
Output is correct |
102 |
Correct |
243 ms |
15816 KB |
Output is correct |
103 |
Correct |
234 ms |
15636 KB |
Output is correct |
104 |
Correct |
196 ms |
15836 KB |
Output is correct |
105 |
Correct |
284 ms |
26432 KB |
Output is correct |
106 |
Correct |
201 ms |
15748 KB |
Output is correct |
107 |
Correct |
189 ms |
15584 KB |
Output is correct |
108 |
Correct |
277 ms |
31104 KB |
Output is correct |
109 |
Correct |
201 ms |
15760 KB |
Output is correct |
110 |
Correct |
277 ms |
31464 KB |
Output is correct |
111 |
Correct |
244 ms |
14860 KB |
Output is correct |