#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n; str s;
vector <vector<int>> v;
ll ans = 0;
ll dfs(int nd, int ss){
ll r = '0'-s[nd];
bool bl = (r==-1);
ll mx = 0;
if(ss == -1 && s[nd] == '1' && v[nd].size() == 1) r = 1;
else if(v[nd].size() <= 2 && s[nd] == '1' && ss != -1) r = 1;
for(int x: v[nd]) if(x != ss){
ll y =dfs(x, nd);
mx = max(mx, y);
r+=y;
}
if(bl && nd == 1) r = max(r, mx+1);
else if(bl) ans = max(ans, mx+1);
ans = max(ans, max(r, (ll)s[nd] - '0'));
if(v[nd].size() <= 2 && s[nd] == '1' && ss != -1)r-=2;
return max(r, (ll)s[nd] - '0');
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
v = vector<vector<int>> (n+1);
for(int i = 1; i < n; i++){
int a, b; cin >> a >> b;
v[a].pb(b);
v[b].pb(a);
}
cin >> s;
s = "x"+s;
ans = max(ans, dfs(1, -1));
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
404 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
1 ms |
512 KB |
Output is correct |
42 |
Correct |
1 ms |
512 KB |
Output is correct |
43 |
Correct |
1 ms |
512 KB |
Output is correct |
44 |
Correct |
1 ms |
512 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
1 ms |
512 KB |
Output is correct |
47 |
Correct |
1 ms |
512 KB |
Output is correct |
48 |
Correct |
2 ms |
512 KB |
Output is correct |
49 |
Correct |
1 ms |
512 KB |
Output is correct |
50 |
Correct |
1 ms |
512 KB |
Output is correct |
51 |
Correct |
1 ms |
512 KB |
Output is correct |
52 |
Correct |
2 ms |
512 KB |
Output is correct |
53 |
Correct |
1 ms |
512 KB |
Output is correct |
54 |
Correct |
1 ms |
512 KB |
Output is correct |
55 |
Correct |
1 ms |
512 KB |
Output is correct |
56 |
Correct |
1 ms |
512 KB |
Output is correct |
57 |
Correct |
2 ms |
640 KB |
Output is correct |
58 |
Correct |
1 ms |
512 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
512 KB |
Output is correct |
61 |
Correct |
1 ms |
512 KB |
Output is correct |
62 |
Correct |
1 ms |
512 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
404 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
1 ms |
512 KB |
Output is correct |
42 |
Correct |
1 ms |
512 KB |
Output is correct |
43 |
Correct |
1 ms |
512 KB |
Output is correct |
44 |
Correct |
1 ms |
512 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
1 ms |
512 KB |
Output is correct |
47 |
Correct |
1 ms |
512 KB |
Output is correct |
48 |
Correct |
2 ms |
512 KB |
Output is correct |
49 |
Correct |
1 ms |
512 KB |
Output is correct |
50 |
Correct |
1 ms |
512 KB |
Output is correct |
51 |
Correct |
1 ms |
512 KB |
Output is correct |
52 |
Correct |
2 ms |
512 KB |
Output is correct |
53 |
Correct |
1 ms |
512 KB |
Output is correct |
54 |
Correct |
1 ms |
512 KB |
Output is correct |
55 |
Correct |
1 ms |
512 KB |
Output is correct |
56 |
Correct |
1 ms |
512 KB |
Output is correct |
57 |
Correct |
2 ms |
640 KB |
Output is correct |
58 |
Correct |
1 ms |
512 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
512 KB |
Output is correct |
61 |
Correct |
1 ms |
512 KB |
Output is correct |
62 |
Correct |
1 ms |
512 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
181 ms |
11964 KB |
Output is correct |
65 |
Correct |
182 ms |
14648 KB |
Output is correct |
66 |
Correct |
175 ms |
14628 KB |
Output is correct |
67 |
Correct |
170 ms |
14632 KB |
Output is correct |
68 |
Correct |
176 ms |
14628 KB |
Output is correct |
69 |
Correct |
174 ms |
14644 KB |
Output is correct |
70 |
Correct |
170 ms |
14660 KB |
Output is correct |
71 |
Correct |
167 ms |
14660 KB |
Output is correct |
72 |
Correct |
174 ms |
14644 KB |
Output is correct |
73 |
Correct |
199 ms |
29556 KB |
Output is correct |
74 |
Correct |
200 ms |
29556 KB |
Output is correct |
75 |
Correct |
170 ms |
14520 KB |
Output is correct |
76 |
Correct |
169 ms |
14528 KB |
Output is correct |
77 |
Correct |
176 ms |
14516 KB |
Output is correct |
78 |
Correct |
177 ms |
14520 KB |
Output is correct |
79 |
Correct |
178 ms |
14532 KB |
Output is correct |
80 |
Correct |
176 ms |
14520 KB |
Output is correct |
81 |
Correct |
154 ms |
14836 KB |
Output is correct |
82 |
Correct |
161 ms |
14712 KB |
Output is correct |
83 |
Correct |
182 ms |
20600 KB |
Output is correct |
84 |
Correct |
185 ms |
23172 KB |
Output is correct |
85 |
Correct |
182 ms |
22784 KB |
Output is correct |
86 |
Correct |
171 ms |
16116 KB |
Output is correct |
87 |
Correct |
170 ms |
15332 KB |
Output is correct |
88 |
Correct |
176 ms |
15792 KB |
Output is correct |
89 |
Correct |
176 ms |
15748 KB |
Output is correct |
90 |
Correct |
167 ms |
15576 KB |
Output is correct |
91 |
Correct |
175 ms |
15832 KB |
Output is correct |
92 |
Correct |
178 ms |
16356 KB |
Output is correct |
93 |
Correct |
160 ms |
19136 KB |
Output is correct |
94 |
Correct |
163 ms |
21360 KB |
Output is correct |
95 |
Correct |
157 ms |
19756 KB |
Output is correct |
96 |
Correct |
158 ms |
17504 KB |
Output is correct |
97 |
Correct |
152 ms |
15696 KB |
Output is correct |
98 |
Correct |
156 ms |
15092 KB |
Output is correct |
99 |
Correct |
169 ms |
15028 KB |
Output is correct |
100 |
Correct |
174 ms |
18532 KB |
Output is correct |
101 |
Correct |
169 ms |
16112 KB |
Output is correct |
102 |
Correct |
161 ms |
15296 KB |
Output is correct |
103 |
Correct |
161 ms |
15084 KB |
Output is correct |
104 |
Correct |
109 ms |
15340 KB |
Output is correct |
105 |
Correct |
199 ms |
25716 KB |
Output is correct |
106 |
Correct |
115 ms |
15212 KB |
Output is correct |
107 |
Correct |
111 ms |
15212 KB |
Output is correct |
108 |
Correct |
206 ms |
30528 KB |
Output is correct |
109 |
Correct |
121 ms |
15368 KB |
Output is correct |
110 |
Correct |
204 ms |
30836 KB |
Output is correct |
111 |
Correct |
169 ms |
14536 KB |
Output is correct |