#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> gra;
string s;int n;
pair<int,int> dfs(int x, int p){
int boff = (s[x] == '1'), bon = (s[x] == '1');
int su = 0;
for(auto i : gra[x]){
if(i == p)continue;
auto now = dfs(i,x);
boff = max({boff,now.first,now.second+(s[x] == '1')});
su+=now.second;
}
bon = max(bon,su-(s[x] == '1'));
boff = max(boff,bon);
return {boff,bon};
}
int main(){
cin >> n;
gra.resize(n);
for(int i = 0; i < n-1; ++i){
int a, b; cin >> a >> b;
a--; b--;
gra[a].push_back(b);
gra[b].push_back(a);
}
cin >> s;
cout << dfs(0,0).first << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
416 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
416 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
3 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
384 KB |
Output is correct |
35 |
Correct |
3 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
380 KB |
Output is correct |
37 |
Correct |
3 ms |
384 KB |
Output is correct |
38 |
Correct |
3 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
512 KB |
Output is correct |
41 |
Correct |
3 ms |
512 KB |
Output is correct |
42 |
Correct |
3 ms |
512 KB |
Output is correct |
43 |
Correct |
3 ms |
512 KB |
Output is correct |
44 |
Correct |
3 ms |
512 KB |
Output is correct |
45 |
Correct |
3 ms |
512 KB |
Output is correct |
46 |
Correct |
3 ms |
512 KB |
Output is correct |
47 |
Correct |
3 ms |
512 KB |
Output is correct |
48 |
Correct |
3 ms |
512 KB |
Output is correct |
49 |
Correct |
3 ms |
512 KB |
Output is correct |
50 |
Correct |
3 ms |
512 KB |
Output is correct |
51 |
Correct |
3 ms |
512 KB |
Output is correct |
52 |
Correct |
3 ms |
512 KB |
Output is correct |
53 |
Correct |
3 ms |
512 KB |
Output is correct |
54 |
Correct |
3 ms |
512 KB |
Output is correct |
55 |
Correct |
3 ms |
512 KB |
Output is correct |
56 |
Correct |
3 ms |
512 KB |
Output is correct |
57 |
Correct |
3 ms |
640 KB |
Output is correct |
58 |
Correct |
3 ms |
532 KB |
Output is correct |
59 |
Correct |
3 ms |
512 KB |
Output is correct |
60 |
Correct |
3 ms |
512 KB |
Output is correct |
61 |
Correct |
3 ms |
512 KB |
Output is correct |
62 |
Correct |
3 ms |
512 KB |
Output is correct |
63 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
416 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
3 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
3 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
384 KB |
Output is correct |
35 |
Correct |
3 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
380 KB |
Output is correct |
37 |
Correct |
3 ms |
384 KB |
Output is correct |
38 |
Correct |
3 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
512 KB |
Output is correct |
41 |
Correct |
3 ms |
512 KB |
Output is correct |
42 |
Correct |
3 ms |
512 KB |
Output is correct |
43 |
Correct |
3 ms |
512 KB |
Output is correct |
44 |
Correct |
3 ms |
512 KB |
Output is correct |
45 |
Correct |
3 ms |
512 KB |
Output is correct |
46 |
Correct |
3 ms |
512 KB |
Output is correct |
47 |
Correct |
3 ms |
512 KB |
Output is correct |
48 |
Correct |
3 ms |
512 KB |
Output is correct |
49 |
Correct |
3 ms |
512 KB |
Output is correct |
50 |
Correct |
3 ms |
512 KB |
Output is correct |
51 |
Correct |
3 ms |
512 KB |
Output is correct |
52 |
Correct |
3 ms |
512 KB |
Output is correct |
53 |
Correct |
3 ms |
512 KB |
Output is correct |
54 |
Correct |
3 ms |
512 KB |
Output is correct |
55 |
Correct |
3 ms |
512 KB |
Output is correct |
56 |
Correct |
3 ms |
512 KB |
Output is correct |
57 |
Correct |
3 ms |
640 KB |
Output is correct |
58 |
Correct |
3 ms |
532 KB |
Output is correct |
59 |
Correct |
3 ms |
512 KB |
Output is correct |
60 |
Correct |
3 ms |
512 KB |
Output is correct |
61 |
Correct |
3 ms |
512 KB |
Output is correct |
62 |
Correct |
3 ms |
512 KB |
Output is correct |
63 |
Correct |
3 ms |
384 KB |
Output is correct |
64 |
Correct |
363 ms |
12280 KB |
Output is correct |
65 |
Correct |
370 ms |
12280 KB |
Output is correct |
66 |
Correct |
372 ms |
12292 KB |
Output is correct |
67 |
Correct |
363 ms |
12296 KB |
Output is correct |
68 |
Correct |
363 ms |
12280 KB |
Output is correct |
69 |
Correct |
364 ms |
12280 KB |
Output is correct |
70 |
Correct |
367 ms |
12352 KB |
Output is correct |
71 |
Correct |
368 ms |
12280 KB |
Output is correct |
72 |
Correct |
367 ms |
12308 KB |
Output is correct |
73 |
Correct |
392 ms |
24956 KB |
Output is correct |
74 |
Correct |
386 ms |
24740 KB |
Output is correct |
75 |
Correct |
364 ms |
12328 KB |
Output is correct |
76 |
Correct |
364 ms |
12168 KB |
Output is correct |
77 |
Correct |
373 ms |
12296 KB |
Output is correct |
78 |
Correct |
367 ms |
12168 KB |
Output is correct |
79 |
Correct |
374 ms |
12168 KB |
Output is correct |
80 |
Correct |
369 ms |
12196 KB |
Output is correct |
81 |
Correct |
338 ms |
12656 KB |
Output is correct |
82 |
Correct |
352 ms |
12532 KB |
Output is correct |
83 |
Correct |
380 ms |
17284 KB |
Output is correct |
84 |
Correct |
376 ms |
19536 KB |
Output is correct |
85 |
Correct |
381 ms |
19080 KB |
Output is correct |
86 |
Correct |
374 ms |
13832 KB |
Output is correct |
87 |
Correct |
374 ms |
12936 KB |
Output is correct |
88 |
Correct |
355 ms |
13448 KB |
Output is correct |
89 |
Correct |
383 ms |
13588 KB |
Output is correct |
90 |
Correct |
349 ms |
13392 KB |
Output is correct |
91 |
Correct |
349 ms |
13448 KB |
Output is correct |
92 |
Correct |
352 ms |
13832 KB |
Output is correct |
93 |
Correct |
342 ms |
16260 KB |
Output is correct |
94 |
Correct |
354 ms |
18180 KB |
Output is correct |
95 |
Correct |
346 ms |
16772 KB |
Output is correct |
96 |
Correct |
350 ms |
14724 KB |
Output is correct |
97 |
Correct |
342 ms |
13320 KB |
Output is correct |
98 |
Correct |
351 ms |
12808 KB |
Output is correct |
99 |
Correct |
361 ms |
12552 KB |
Output is correct |
100 |
Correct |
353 ms |
15752 KB |
Output is correct |
101 |
Correct |
358 ms |
13704 KB |
Output is correct |
102 |
Correct |
362 ms |
12992 KB |
Output is correct |
103 |
Correct |
358 ms |
12808 KB |
Output is correct |
104 |
Correct |
299 ms |
13036 KB |
Output is correct |
105 |
Correct |
377 ms |
21640 KB |
Output is correct |
106 |
Correct |
288 ms |
13036 KB |
Output is correct |
107 |
Correct |
284 ms |
13036 KB |
Output is correct |
108 |
Correct |
383 ms |
25612 KB |
Output is correct |
109 |
Correct |
299 ms |
13036 KB |
Output is correct |
110 |
Correct |
379 ms |
25864 KB |
Output is correct |
111 |
Correct |
348 ms |
12152 KB |
Output is correct |