#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
#define F first
#define S second
ll min(ll x, ll y){
return ((x < y) ? x : y);
}
ll max(ll x, ll y){
return ((x > y) ? x : y);
}
class Tree {
private:
ll n;
vi memo,p,b,sz;
vvi adjList;
void DFS (ll v){
//cout << v << endl;
sz[v] += b[v];
for (auto u: adjList[v]){
if (u == p[v]) continue;
p[u] = v;
DFS(u);
sz[v] += sz[u];
}
}
ll dp(ll v){
if (memo[v] != -1) return memo[v];
memo[v] = -b[v];
for (auto u: adjList[v]){
//cout << v << " -> " << u << endl;
if (u == p[v]) continue;
memo[v] += dp(u);
}
memo[v] = max(memo[v],b[v]);
//cout << v << ": " << memo[v] << endl;
return memo[v];
}
public:
Tree(ll _n){
n = _n;
p.assign(n,-1);
memo.assign(n,-1);
adjList.resize(n);
sz.assign(n,0);
}
void add_edge(ll u, ll v){
adjList[u].push_back(v);
adjList[v].push_back(u);
}
void set_b(vi _b){
b.resize(n);
b = _b;
}
ll solve(){
DFS(0);
dp(0);
ll res = 0;
/*
cout << "sz: \n";
for (auto i: sz) cout << i << " ";
cout << endl;
*/
for (ll i = 0; n > i; i++){
ll cur = memo[i];
if (sz[0] != sz[i]) cur++;
//cout << i << ": " << cur << endl;
res = max(res,cur);
}
return res;
}
};
int main(){
ll n; cin >> n;
Tree tree(n);
for (ll i = 0; n-1 > i; i++){
ll u,v;
cin >> u >> v;
u--; v--;
tree.add_edge(u,v);
}
vi b;
b.resize(n);
for (ll i = 0; n > i; i++){
char c; cin >> c;
b[i] = (c == '1');
}
tree.set_b(b);
cout << tree.solve() << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
288 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
292 KB |
Output is correct |
14 |
Correct |
1 ms |
276 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
288 KB |
Output is correct |
17 |
Correct |
0 ms |
268 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
288 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
292 KB |
Output is correct |
14 |
Correct |
1 ms |
276 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
288 KB |
Output is correct |
17 |
Correct |
0 ms |
268 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
464 KB |
Output is correct |
23 |
Correct |
2 ms |
420 KB |
Output is correct |
24 |
Correct |
2 ms |
432 KB |
Output is correct |
25 |
Correct |
2 ms |
432 KB |
Output is correct |
26 |
Correct |
2 ms |
456 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
2 ms |
440 KB |
Output is correct |
31 |
Correct |
2 ms |
432 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
432 KB |
Output is correct |
34 |
Correct |
2 ms |
432 KB |
Output is correct |
35 |
Correct |
2 ms |
436 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
424 KB |
Output is correct |
38 |
Correct |
2 ms |
460 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
2 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
2 ms |
460 KB |
Output is correct |
43 |
Correct |
2 ms |
428 KB |
Output is correct |
44 |
Correct |
2 ms |
432 KB |
Output is correct |
45 |
Correct |
2 ms |
460 KB |
Output is correct |
46 |
Correct |
2 ms |
432 KB |
Output is correct |
47 |
Correct |
2 ms |
460 KB |
Output is correct |
48 |
Correct |
2 ms |
460 KB |
Output is correct |
49 |
Correct |
2 ms |
468 KB |
Output is correct |
50 |
Correct |
2 ms |
428 KB |
Output is correct |
51 |
Correct |
2 ms |
460 KB |
Output is correct |
52 |
Correct |
2 ms |
464 KB |
Output is correct |
53 |
Correct |
2 ms |
452 KB |
Output is correct |
54 |
Correct |
2 ms |
432 KB |
Output is correct |
55 |
Correct |
2 ms |
460 KB |
Output is correct |
56 |
Correct |
2 ms |
444 KB |
Output is correct |
57 |
Correct |
2 ms |
440 KB |
Output is correct |
58 |
Correct |
2 ms |
460 KB |
Output is correct |
59 |
Correct |
2 ms |
432 KB |
Output is correct |
60 |
Correct |
2 ms |
460 KB |
Output is correct |
61 |
Correct |
2 ms |
460 KB |
Output is correct |
62 |
Correct |
2 ms |
564 KB |
Output is correct |
63 |
Correct |
2 ms |
428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
288 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
292 KB |
Output is correct |
14 |
Correct |
1 ms |
276 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
288 KB |
Output is correct |
17 |
Correct |
0 ms |
268 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
464 KB |
Output is correct |
23 |
Correct |
2 ms |
420 KB |
Output is correct |
24 |
Correct |
2 ms |
432 KB |
Output is correct |
25 |
Correct |
2 ms |
432 KB |
Output is correct |
26 |
Correct |
2 ms |
456 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
2 ms |
440 KB |
Output is correct |
31 |
Correct |
2 ms |
432 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
432 KB |
Output is correct |
34 |
Correct |
2 ms |
432 KB |
Output is correct |
35 |
Correct |
2 ms |
436 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
424 KB |
Output is correct |
38 |
Correct |
2 ms |
460 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
2 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
2 ms |
460 KB |
Output is correct |
43 |
Correct |
2 ms |
428 KB |
Output is correct |
44 |
Correct |
2 ms |
432 KB |
Output is correct |
45 |
Correct |
2 ms |
460 KB |
Output is correct |
46 |
Correct |
2 ms |
432 KB |
Output is correct |
47 |
Correct |
2 ms |
460 KB |
Output is correct |
48 |
Correct |
2 ms |
460 KB |
Output is correct |
49 |
Correct |
2 ms |
468 KB |
Output is correct |
50 |
Correct |
2 ms |
428 KB |
Output is correct |
51 |
Correct |
2 ms |
460 KB |
Output is correct |
52 |
Correct |
2 ms |
464 KB |
Output is correct |
53 |
Correct |
2 ms |
452 KB |
Output is correct |
54 |
Correct |
2 ms |
432 KB |
Output is correct |
55 |
Correct |
2 ms |
460 KB |
Output is correct |
56 |
Correct |
2 ms |
444 KB |
Output is correct |
57 |
Correct |
2 ms |
440 KB |
Output is correct |
58 |
Correct |
2 ms |
460 KB |
Output is correct |
59 |
Correct |
2 ms |
432 KB |
Output is correct |
60 |
Correct |
2 ms |
460 KB |
Output is correct |
61 |
Correct |
2 ms |
460 KB |
Output is correct |
62 |
Correct |
2 ms |
564 KB |
Output is correct |
63 |
Correct |
2 ms |
428 KB |
Output is correct |
64 |
Correct |
250 ms |
24324 KB |
Output is correct |
65 |
Correct |
248 ms |
24416 KB |
Output is correct |
66 |
Correct |
246 ms |
24360 KB |
Output is correct |
67 |
Correct |
235 ms |
24376 KB |
Output is correct |
68 |
Correct |
239 ms |
24356 KB |
Output is correct |
69 |
Correct |
244 ms |
24644 KB |
Output is correct |
70 |
Correct |
234 ms |
24332 KB |
Output is correct |
71 |
Correct |
277 ms |
24412 KB |
Output is correct |
72 |
Correct |
257 ms |
24368 KB |
Output is correct |
73 |
Correct |
266 ms |
27696 KB |
Output is correct |
74 |
Correct |
267 ms |
27688 KB |
Output is correct |
75 |
Correct |
232 ms |
25648 KB |
Output is correct |
76 |
Correct |
248 ms |
25692 KB |
Output is correct |
77 |
Correct |
247 ms |
24376 KB |
Output is correct |
78 |
Correct |
240 ms |
24328 KB |
Output is correct |
79 |
Correct |
260 ms |
24516 KB |
Output is correct |
80 |
Correct |
243 ms |
24420 KB |
Output is correct |
81 |
Correct |
204 ms |
24376 KB |
Output is correct |
82 |
Correct |
227 ms |
24356 KB |
Output is correct |
83 |
Correct |
272 ms |
25240 KB |
Output is correct |
84 |
Correct |
256 ms |
26108 KB |
Output is correct |
85 |
Correct |
268 ms |
26056 KB |
Output is correct |
86 |
Correct |
253 ms |
24360 KB |
Output is correct |
87 |
Correct |
242 ms |
24476 KB |
Output is correct |
88 |
Correct |
218 ms |
24660 KB |
Output is correct |
89 |
Correct |
221 ms |
24624 KB |
Output is correct |
90 |
Correct |
240 ms |
24692 KB |
Output is correct |
91 |
Correct |
227 ms |
24612 KB |
Output is correct |
92 |
Correct |
224 ms |
24652 KB |
Output is correct |
93 |
Correct |
215 ms |
24224 KB |
Output is correct |
94 |
Correct |
225 ms |
24940 KB |
Output is correct |
95 |
Correct |
224 ms |
24492 KB |
Output is correct |
96 |
Correct |
226 ms |
24176 KB |
Output is correct |
97 |
Correct |
221 ms |
24344 KB |
Output is correct |
98 |
Correct |
224 ms |
24484 KB |
Output is correct |
99 |
Correct |
245 ms |
24588 KB |
Output is correct |
100 |
Correct |
221 ms |
24272 KB |
Output is correct |
101 |
Correct |
222 ms |
24132 KB |
Output is correct |
102 |
Correct |
225 ms |
24368 KB |
Output is correct |
103 |
Correct |
228 ms |
24704 KB |
Output is correct |
104 |
Correct |
168 ms |
25080 KB |
Output is correct |
105 |
Correct |
250 ms |
26124 KB |
Output is correct |
106 |
Correct |
163 ms |
24728 KB |
Output is correct |
107 |
Correct |
170 ms |
24820 KB |
Output is correct |
108 |
Correct |
267 ms |
28076 KB |
Output is correct |
109 |
Correct |
182 ms |
25008 KB |
Output is correct |
110 |
Correct |
267 ms |
28224 KB |
Output is correct |
111 |
Correct |
216 ms |
24348 KB |
Output is correct |