#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
#define g4(a) get<4>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
using iiiii = tuple<int, int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, mem[200005][2];
char c;
bool S[200005];
vector<int> adj[200005];
int dp(int n, bool b, int e = -1) {
if (mem[n][b] != -1) return mem[n][b];
if (S[n]) {
if (b) {
int x = -1;
for (auto u : adj[n]) if (u != e) {
x += dp(u, 1, n);
}
return mem[n][b] = max(x, 1ll);
} else {
int x = 0, y = 0, z = 0;
for (auto u : adj[n]) if (u != e) {
x = max(x, dp(u, 1, n));
y += dp(u, 1, n);
z = max(z, dp(u, 0, n));
}
x++;
y--;
return mem[n][b] = max({x, y, z});
}
} else {
if (b) {
int x = 0;
for (auto u : adj[n]) if (u != e) {
x += dp(u, 1, n);
}
return mem[n][b] = max(x, 0ll);
} else {
int x = 0, y = 0, z = 0;
for (auto u : adj[n]) if (u != e) {
x = max(x, dp(u, 1, n));
y += dp(u, 1, n);
z = max(z, dp(u, 0, n));
}
return mem[n][b] = max({x, y, z});
}
}
}
main() {
memset(mem, -1, sizeof mem);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int u, v, i = 1; i < N; i++) {
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 1; i <= N; i++) {
cin >> c;
S[i] = (c == '1');
}
cout << dp(1, 0) << '\n';
}
Compilation message
power.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
72 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8152 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
5 ms |
8148 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8156 KB |
Output is correct |
9 |
Correct |
4 ms |
8152 KB |
Output is correct |
10 |
Correct |
4 ms |
8160 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8152 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8148 KB |
Output is correct |
16 |
Correct |
4 ms |
8160 KB |
Output is correct |
17 |
Correct |
4 ms |
8152 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8156 KB |
Output is correct |
21 |
Correct |
5 ms |
8276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8152 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
5 ms |
8148 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8156 KB |
Output is correct |
9 |
Correct |
4 ms |
8152 KB |
Output is correct |
10 |
Correct |
4 ms |
8160 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8152 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8148 KB |
Output is correct |
16 |
Correct |
4 ms |
8160 KB |
Output is correct |
17 |
Correct |
4 ms |
8152 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8156 KB |
Output is correct |
21 |
Correct |
5 ms |
8276 KB |
Output is correct |
22 |
Correct |
5 ms |
8148 KB |
Output is correct |
23 |
Correct |
5 ms |
8148 KB |
Output is correct |
24 |
Correct |
6 ms |
8148 KB |
Output is correct |
25 |
Correct |
5 ms |
8148 KB |
Output is correct |
26 |
Correct |
5 ms |
8148 KB |
Output is correct |
27 |
Correct |
6 ms |
8296 KB |
Output is correct |
28 |
Correct |
5 ms |
8164 KB |
Output is correct |
29 |
Correct |
5 ms |
8276 KB |
Output is correct |
30 |
Correct |
5 ms |
8220 KB |
Output is correct |
31 |
Correct |
5 ms |
8168 KB |
Output is correct |
32 |
Correct |
5 ms |
8244 KB |
Output is correct |
33 |
Correct |
5 ms |
8224 KB |
Output is correct |
34 |
Correct |
5 ms |
8148 KB |
Output is correct |
35 |
Correct |
5 ms |
8148 KB |
Output is correct |
36 |
Correct |
5 ms |
8148 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
5 ms |
8204 KB |
Output is correct |
39 |
Correct |
6 ms |
8276 KB |
Output is correct |
40 |
Correct |
5 ms |
8276 KB |
Output is correct |
41 |
Correct |
5 ms |
8296 KB |
Output is correct |
42 |
Correct |
5 ms |
8276 KB |
Output is correct |
43 |
Correct |
7 ms |
8148 KB |
Output is correct |
44 |
Correct |
5 ms |
8276 KB |
Output is correct |
45 |
Correct |
5 ms |
8264 KB |
Output is correct |
46 |
Correct |
5 ms |
8192 KB |
Output is correct |
47 |
Correct |
5 ms |
8200 KB |
Output is correct |
48 |
Correct |
5 ms |
8216 KB |
Output is correct |
49 |
Correct |
5 ms |
8292 KB |
Output is correct |
50 |
Correct |
5 ms |
8216 KB |
Output is correct |
51 |
Correct |
5 ms |
8276 KB |
Output is correct |
52 |
Correct |
5 ms |
8148 KB |
Output is correct |
53 |
Correct |
5 ms |
8276 KB |
Output is correct |
54 |
Correct |
5 ms |
8168 KB |
Output is correct |
55 |
Correct |
5 ms |
8164 KB |
Output is correct |
56 |
Correct |
5 ms |
8148 KB |
Output is correct |
57 |
Correct |
5 ms |
8292 KB |
Output is correct |
58 |
Correct |
5 ms |
8216 KB |
Output is correct |
59 |
Correct |
5 ms |
8256 KB |
Output is correct |
60 |
Correct |
4 ms |
8276 KB |
Output is correct |
61 |
Correct |
5 ms |
8148 KB |
Output is correct |
62 |
Correct |
5 ms |
8276 KB |
Output is correct |
63 |
Correct |
5 ms |
8164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8152 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
5 ms |
8148 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8156 KB |
Output is correct |
9 |
Correct |
4 ms |
8152 KB |
Output is correct |
10 |
Correct |
4 ms |
8160 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8152 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8148 KB |
Output is correct |
16 |
Correct |
4 ms |
8160 KB |
Output is correct |
17 |
Correct |
4 ms |
8152 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8156 KB |
Output is correct |
21 |
Correct |
5 ms |
8276 KB |
Output is correct |
22 |
Correct |
5 ms |
8148 KB |
Output is correct |
23 |
Correct |
5 ms |
8148 KB |
Output is correct |
24 |
Correct |
6 ms |
8148 KB |
Output is correct |
25 |
Correct |
5 ms |
8148 KB |
Output is correct |
26 |
Correct |
5 ms |
8148 KB |
Output is correct |
27 |
Correct |
6 ms |
8296 KB |
Output is correct |
28 |
Correct |
5 ms |
8164 KB |
Output is correct |
29 |
Correct |
5 ms |
8276 KB |
Output is correct |
30 |
Correct |
5 ms |
8220 KB |
Output is correct |
31 |
Correct |
5 ms |
8168 KB |
Output is correct |
32 |
Correct |
5 ms |
8244 KB |
Output is correct |
33 |
Correct |
5 ms |
8224 KB |
Output is correct |
34 |
Correct |
5 ms |
8148 KB |
Output is correct |
35 |
Correct |
5 ms |
8148 KB |
Output is correct |
36 |
Correct |
5 ms |
8148 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
5 ms |
8204 KB |
Output is correct |
39 |
Correct |
6 ms |
8276 KB |
Output is correct |
40 |
Correct |
5 ms |
8276 KB |
Output is correct |
41 |
Correct |
5 ms |
8296 KB |
Output is correct |
42 |
Correct |
5 ms |
8276 KB |
Output is correct |
43 |
Correct |
7 ms |
8148 KB |
Output is correct |
44 |
Correct |
5 ms |
8276 KB |
Output is correct |
45 |
Correct |
5 ms |
8264 KB |
Output is correct |
46 |
Correct |
5 ms |
8192 KB |
Output is correct |
47 |
Correct |
5 ms |
8200 KB |
Output is correct |
48 |
Correct |
5 ms |
8216 KB |
Output is correct |
49 |
Correct |
5 ms |
8292 KB |
Output is correct |
50 |
Correct |
5 ms |
8216 KB |
Output is correct |
51 |
Correct |
5 ms |
8276 KB |
Output is correct |
52 |
Correct |
5 ms |
8148 KB |
Output is correct |
53 |
Correct |
5 ms |
8276 KB |
Output is correct |
54 |
Correct |
5 ms |
8168 KB |
Output is correct |
55 |
Correct |
5 ms |
8164 KB |
Output is correct |
56 |
Correct |
5 ms |
8148 KB |
Output is correct |
57 |
Correct |
5 ms |
8292 KB |
Output is correct |
58 |
Correct |
5 ms |
8216 KB |
Output is correct |
59 |
Correct |
5 ms |
8256 KB |
Output is correct |
60 |
Correct |
4 ms |
8276 KB |
Output is correct |
61 |
Correct |
5 ms |
8148 KB |
Output is correct |
62 |
Correct |
5 ms |
8276 KB |
Output is correct |
63 |
Correct |
5 ms |
8164 KB |
Output is correct |
64 |
Correct |
159 ms |
18288 KB |
Output is correct |
65 |
Correct |
184 ms |
18412 KB |
Output is correct |
66 |
Correct |
152 ms |
18508 KB |
Output is correct |
67 |
Correct |
141 ms |
18424 KB |
Output is correct |
68 |
Correct |
162 ms |
18380 KB |
Output is correct |
69 |
Correct |
168 ms |
18404 KB |
Output is correct |
70 |
Correct |
150 ms |
18408 KB |
Output is correct |
71 |
Correct |
174 ms |
18408 KB |
Output is correct |
72 |
Correct |
153 ms |
18532 KB |
Output is correct |
73 |
Correct |
193 ms |
32588 KB |
Output is correct |
74 |
Correct |
201 ms |
32460 KB |
Output is correct |
75 |
Correct |
153 ms |
19628 KB |
Output is correct |
76 |
Correct |
178 ms |
19752 KB |
Output is correct |
77 |
Correct |
188 ms |
18376 KB |
Output is correct |
78 |
Correct |
166 ms |
18292 KB |
Output is correct |
79 |
Correct |
178 ms |
18268 KB |
Output is correct |
80 |
Correct |
177 ms |
18388 KB |
Output is correct |
81 |
Correct |
130 ms |
18296 KB |
Output is correct |
82 |
Correct |
152 ms |
18248 KB |
Output is correct |
83 |
Correct |
180 ms |
24540 KB |
Output is correct |
84 |
Correct |
188 ms |
27084 KB |
Output is correct |
85 |
Correct |
195 ms |
26708 KB |
Output is correct |
86 |
Correct |
168 ms |
19968 KB |
Output is correct |
87 |
Correct |
157 ms |
19320 KB |
Output is correct |
88 |
Correct |
157 ms |
19320 KB |
Output is correct |
89 |
Correct |
149 ms |
19356 KB |
Output is correct |
90 |
Correct |
154 ms |
19184 KB |
Output is correct |
91 |
Correct |
142 ms |
19404 KB |
Output is correct |
92 |
Correct |
160 ms |
19788 KB |
Output is correct |
93 |
Correct |
161 ms |
22468 KB |
Output is correct |
94 |
Correct |
144 ms |
24516 KB |
Output is correct |
95 |
Correct |
140 ms |
22948 KB |
Output is correct |
96 |
Correct |
155 ms |
21024 KB |
Output is correct |
97 |
Correct |
136 ms |
19020 KB |
Output is correct |
98 |
Correct |
139 ms |
18508 KB |
Output is correct |
99 |
Correct |
161 ms |
18492 KB |
Output is correct |
100 |
Correct |
149 ms |
22092 KB |
Output is correct |
101 |
Correct |
172 ms |
19600 KB |
Output is correct |
102 |
Correct |
157 ms |
18664 KB |
Output is correct |
103 |
Correct |
162 ms |
18640 KB |
Output is correct |
104 |
Correct |
90 ms |
19004 KB |
Output is correct |
105 |
Correct |
184 ms |
28796 KB |
Output is correct |
106 |
Correct |
70 ms |
18708 KB |
Output is correct |
107 |
Correct |
98 ms |
18804 KB |
Output is correct |
108 |
Correct |
179 ms |
33492 KB |
Output is correct |
109 |
Correct |
73 ms |
18924 KB |
Output is correct |
110 |
Correct |
176 ms |
33996 KB |
Output is correct |
111 |
Correct |
158 ms |
18352 KB |
Output is correct |