#include <bits/stdc++.h>
#define MN 200071
using namespace std;
int n;
vector<int> L[MN];
bitset<MN> On;
using ii = pair<int, int>;
ii dp(int u, int p) {
int r1 = 0, r2 = 0, r3;
vector<ii> F;
for(auto it : L[u])
if(it != p) F.push_back(dp(it, u));
//ceva de sus
if(On[u - 1]) {
r1 = -1; // il sparg
for(auto [a, b] : F) r1 += a;
r1 = max(r1, 1);
} else
for(auto [a, b] : F) r1 += a;
//nimic de sus
if(On[u - 1]) {
int sa = 0, bb = 0, ba = 0;
for(auto [a, b] : F) {
bb = max(bb, b);
ba = max(ba, a);
sa += a;
}
r2 = max(bb, max(sa - 1, ba + 1));
r2 = max(r2, 1);
} else {
int sa = 0, bb = 0;
for(auto [a, b] : F) {
bb = max(bb, b);
sa += a;
}
r2 = max(bb, sa);
}
return {r1, r2};
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
L[u].push_back(v);
L[v].push_back(u);
}
string a;
cin >> a;
for(int i = 0; i < a.size(); ++i)
On[i] = (a[i] == '1');
auto [b, c] = dp(1, 0);
cout << max(b, c) << "\n";
return 0;
}
Compilation message
power.cpp: In function 'ii dp(int, int)':
power.cpp:10:22: warning: unused variable 'r3' [-Wunused-variable]
10 | int r1 = 0, r2 = 0, r3;
| ^~
power.cpp: In function 'int main()':
power.cpp:53:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 0; i < a.size(); ++i)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4924 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5028 KB |
Output is correct |
20 |
Correct |
3 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
5028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4924 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5028 KB |
Output is correct |
20 |
Correct |
3 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
5028 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5040 KB |
Output is correct |
28 |
Correct |
4 ms |
5036 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5040 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5032 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5112 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5040 KB |
Output is correct |
39 |
Correct |
4 ms |
5136 KB |
Output is correct |
40 |
Correct |
5 ms |
5076 KB |
Output is correct |
41 |
Correct |
4 ms |
5164 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5040 KB |
Output is correct |
45 |
Correct |
4 ms |
5044 KB |
Output is correct |
46 |
Correct |
4 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5040 KB |
Output is correct |
48 |
Correct |
4 ms |
5220 KB |
Output is correct |
49 |
Correct |
5 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5080 KB |
Output is correct |
51 |
Correct |
4 ms |
5036 KB |
Output is correct |
52 |
Correct |
3 ms |
5036 KB |
Output is correct |
53 |
Correct |
3 ms |
5040 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
4 ms |
5032 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5216 KB |
Output is correct |
58 |
Correct |
4 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
4 ms |
5076 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4924 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5028 KB |
Output is correct |
20 |
Correct |
3 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
5028 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5040 KB |
Output is correct |
28 |
Correct |
4 ms |
5036 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5040 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5032 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5112 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5040 KB |
Output is correct |
39 |
Correct |
4 ms |
5136 KB |
Output is correct |
40 |
Correct |
5 ms |
5076 KB |
Output is correct |
41 |
Correct |
4 ms |
5164 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5040 KB |
Output is correct |
45 |
Correct |
4 ms |
5044 KB |
Output is correct |
46 |
Correct |
4 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5040 KB |
Output is correct |
48 |
Correct |
4 ms |
5220 KB |
Output is correct |
49 |
Correct |
5 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5080 KB |
Output is correct |
51 |
Correct |
4 ms |
5036 KB |
Output is correct |
52 |
Correct |
3 ms |
5036 KB |
Output is correct |
53 |
Correct |
3 ms |
5040 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
4 ms |
5032 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5216 KB |
Output is correct |
58 |
Correct |
4 ms |
5076 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
4 ms |
5076 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
64 |
Correct |
121 ms |
14456 KB |
Output is correct |
65 |
Correct |
116 ms |
14448 KB |
Output is correct |
66 |
Correct |
127 ms |
14512 KB |
Output is correct |
67 |
Correct |
119 ms |
14472 KB |
Output is correct |
68 |
Correct |
120 ms |
14480 KB |
Output is correct |
69 |
Correct |
122 ms |
14460 KB |
Output is correct |
70 |
Correct |
121 ms |
14472 KB |
Output is correct |
71 |
Correct |
119 ms |
14416 KB |
Output is correct |
72 |
Correct |
120 ms |
14484 KB |
Output is correct |
73 |
Correct |
141 ms |
29728 KB |
Output is correct |
74 |
Correct |
136 ms |
29492 KB |
Output is correct |
75 |
Correct |
115 ms |
14388 KB |
Output is correct |
76 |
Correct |
116 ms |
14376 KB |
Output is correct |
77 |
Correct |
121 ms |
14440 KB |
Output is correct |
78 |
Correct |
118 ms |
14432 KB |
Output is correct |
79 |
Correct |
122 ms |
14440 KB |
Output is correct |
80 |
Correct |
113 ms |
14456 KB |
Output is correct |
81 |
Correct |
97 ms |
16180 KB |
Output is correct |
82 |
Correct |
111 ms |
15052 KB |
Output is correct |
83 |
Correct |
121 ms |
20952 KB |
Output is correct |
84 |
Correct |
131 ms |
23708 KB |
Output is correct |
85 |
Correct |
131 ms |
23316 KB |
Output is correct |
86 |
Correct |
121 ms |
16116 KB |
Output is correct |
87 |
Correct |
119 ms |
15272 KB |
Output is correct |
88 |
Correct |
111 ms |
15728 KB |
Output is correct |
89 |
Correct |
107 ms |
15756 KB |
Output is correct |
90 |
Correct |
107 ms |
15464 KB |
Output is correct |
91 |
Correct |
110 ms |
15852 KB |
Output is correct |
92 |
Correct |
103 ms |
16224 KB |
Output is correct |
93 |
Correct |
111 ms |
19712 KB |
Output is correct |
94 |
Correct |
111 ms |
21656 KB |
Output is correct |
95 |
Correct |
103 ms |
20040 KB |
Output is correct |
96 |
Correct |
105 ms |
17640 KB |
Output is correct |
97 |
Correct |
97 ms |
15532 KB |
Output is correct |
98 |
Correct |
110 ms |
14940 KB |
Output is correct |
99 |
Correct |
113 ms |
14868 KB |
Output is correct |
100 |
Correct |
109 ms |
18924 KB |
Output is correct |
101 |
Correct |
110 ms |
16144 KB |
Output is correct |
102 |
Correct |
100 ms |
15196 KB |
Output is correct |
103 |
Correct |
108 ms |
15012 KB |
Output is correct |
104 |
Correct |
64 ms |
18236 KB |
Output is correct |
105 |
Correct |
130 ms |
25700 KB |
Output is correct |
106 |
Correct |
68 ms |
17928 KB |
Output is correct |
107 |
Correct |
63 ms |
18032 KB |
Output is correct |
108 |
Correct |
130 ms |
30428 KB |
Output is correct |
109 |
Correct |
64 ms |
18232 KB |
Output is correct |
110 |
Correct |
155 ms |
30788 KB |
Output is correct |
111 |
Correct |
123 ms |
14400 KB |
Output is correct |