#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define FOR(i, x, y) for(ll i=x; i<y; i++)
#define FORNEG(i, x, y) for(ll i=x; i>y; i--)
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL)
ll n;
string sus;
vector<ll> edges[200001];
ll par[200001];
bool visited[200001];
ll dps[200001];
ll ans = 0;
void dfs(ll a){
for (auto&i : edges[a]){
if (!visited[i]){
visited[i] = true;
par[i] = a;
dfs(i);
}
}
}
ll dp(ll a){
if (dps[a] != -1) return dps[a];
ll sum = 0;
ll maxi = 0;
for (auto&i : edges[a]){
if (i==par[a]) continue;
sum += dp(i);
maxi = max(maxi, dp(i));
}
if (sus[a-1] == '1'){
ans = max(ans, max(maxi+1, sum-1));
}else{
ans = max(ans, sum);
}
dps[a] = max(sum - sus[a-1] + '0', ll(sus[a-1] - '0'));
return max(sum - sus[a-1] + '0', ll(sus[a-1] - '0'));
}
int main(){
cin >> n;
if (n==1){
cout << 1;
return 0;
}
FOR(i,0,n-1){
ll a,b;
cin >> a >> b;
edges[a].push_back(b);
edges[b].push_back(a);
}
cin >> sus;
FOR(i,0,n+1) par[i] = -1, visited[i] = 0, dps[i] = -1;
visited[1] = true;
dfs(1);
dp(1);
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 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 |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
5 ms |
4968 KB |
Output is correct |
14 |
Correct |
4 ms |
5032 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 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 |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 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 |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
5 ms |
4968 KB |
Output is correct |
14 |
Correct |
4 ms |
5032 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 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 |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
5 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 |
6 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5148 KB |
Output is correct |
29 |
Correct |
6 ms |
5140 KB |
Output is correct |
30 |
Correct |
6 ms |
5204 KB |
Output is correct |
31 |
Correct |
6 ms |
5156 KB |
Output is correct |
32 |
Correct |
5 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
5 ms |
5092 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
5 ms |
5076 KB |
Output is correct |
39 |
Correct |
5 ms |
5148 KB |
Output is correct |
40 |
Correct |
5 ms |
5164 KB |
Output is correct |
41 |
Correct |
6 ms |
5076 KB |
Output is correct |
42 |
Correct |
5 ms |
5144 KB |
Output is correct |
43 |
Correct |
5 ms |
5140 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5144 KB |
Output is correct |
47 |
Correct |
4 ms |
5144 KB |
Output is correct |
48 |
Correct |
5 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5156 KB |
Output is correct |
51 |
Correct |
4 ms |
5152 KB |
Output is correct |
52 |
Correct |
4 ms |
5076 KB |
Output is correct |
53 |
Correct |
4 ms |
5076 KB |
Output is correct |
54 |
Correct |
4 ms |
5148 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
4 ms |
5148 KB |
Output is correct |
57 |
Correct |
4 ms |
5148 KB |
Output is correct |
58 |
Correct |
5 ms |
5076 KB |
Output is correct |
59 |
Correct |
6 ms |
5148 KB |
Output is correct |
60 |
Correct |
5 ms |
5076 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
5 ms |
5144 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 |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 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 |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
5 ms |
4968 KB |
Output is correct |
14 |
Correct |
4 ms |
5032 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5004 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 |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
5 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 |
6 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5148 KB |
Output is correct |
29 |
Correct |
6 ms |
5140 KB |
Output is correct |
30 |
Correct |
6 ms |
5204 KB |
Output is correct |
31 |
Correct |
6 ms |
5156 KB |
Output is correct |
32 |
Correct |
5 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
5 ms |
5092 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
5 ms |
5076 KB |
Output is correct |
39 |
Correct |
5 ms |
5148 KB |
Output is correct |
40 |
Correct |
5 ms |
5164 KB |
Output is correct |
41 |
Correct |
6 ms |
5076 KB |
Output is correct |
42 |
Correct |
5 ms |
5144 KB |
Output is correct |
43 |
Correct |
5 ms |
5140 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5144 KB |
Output is correct |
47 |
Correct |
4 ms |
5144 KB |
Output is correct |
48 |
Correct |
5 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5156 KB |
Output is correct |
51 |
Correct |
4 ms |
5152 KB |
Output is correct |
52 |
Correct |
4 ms |
5076 KB |
Output is correct |
53 |
Correct |
4 ms |
5076 KB |
Output is correct |
54 |
Correct |
4 ms |
5148 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
4 ms |
5148 KB |
Output is correct |
57 |
Correct |
4 ms |
5148 KB |
Output is correct |
58 |
Correct |
5 ms |
5076 KB |
Output is correct |
59 |
Correct |
6 ms |
5148 KB |
Output is correct |
60 |
Correct |
5 ms |
5076 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
5 ms |
5144 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
64 |
Correct |
227 ms |
16896 KB |
Output is correct |
65 |
Correct |
246 ms |
16816 KB |
Output is correct |
66 |
Correct |
265 ms |
16908 KB |
Output is correct |
67 |
Correct |
269 ms |
16856 KB |
Output is correct |
68 |
Correct |
268 ms |
16824 KB |
Output is correct |
69 |
Correct |
258 ms |
16896 KB |
Output is correct |
70 |
Correct |
273 ms |
17500 KB |
Output is correct |
71 |
Correct |
232 ms |
16924 KB |
Output is correct |
72 |
Correct |
255 ms |
17000 KB |
Output is correct |
73 |
Correct |
291 ms |
25896 KB |
Output is correct |
74 |
Correct |
295 ms |
27828 KB |
Output is correct |
75 |
Correct |
255 ms |
19932 KB |
Output is correct |
76 |
Correct |
287 ms |
19912 KB |
Output is correct |
77 |
Correct |
243 ms |
18608 KB |
Output is correct |
78 |
Correct |
264 ms |
18772 KB |
Output is correct |
79 |
Correct |
278 ms |
18576 KB |
Output is correct |
80 |
Correct |
262 ms |
18676 KB |
Output is correct |
81 |
Correct |
267 ms |
18776 KB |
Output is correct |
82 |
Correct |
230 ms |
18708 KB |
Output is correct |
83 |
Correct |
280 ms |
22764 KB |
Output is correct |
84 |
Correct |
290 ms |
24408 KB |
Output is correct |
85 |
Correct |
292 ms |
24144 KB |
Output is correct |
86 |
Correct |
268 ms |
19688 KB |
Output is correct |
87 |
Correct |
307 ms |
19320 KB |
Output is correct |
88 |
Correct |
264 ms |
19372 KB |
Output is correct |
89 |
Correct |
256 ms |
19460 KB |
Output is correct |
90 |
Correct |
347 ms |
19204 KB |
Output is correct |
91 |
Correct |
264 ms |
19452 KB |
Output is correct |
92 |
Correct |
289 ms |
19744 KB |
Output is correct |
93 |
Correct |
214 ms |
21348 KB |
Output is correct |
94 |
Correct |
260 ms |
22716 KB |
Output is correct |
95 |
Correct |
242 ms |
21788 KB |
Output is correct |
96 |
Correct |
222 ms |
20308 KB |
Output is correct |
97 |
Correct |
232 ms |
19092 KB |
Output is correct |
98 |
Correct |
228 ms |
18852 KB |
Output is correct |
99 |
Correct |
273 ms |
18792 KB |
Output is correct |
100 |
Correct |
230 ms |
21044 KB |
Output is correct |
101 |
Correct |
234 ms |
19304 KB |
Output is correct |
102 |
Correct |
230 ms |
18912 KB |
Output is correct |
103 |
Correct |
238 ms |
18928 KB |
Output is correct |
104 |
Correct |
184 ms |
19384 KB |
Output is correct |
105 |
Correct |
254 ms |
25204 KB |
Output is correct |
106 |
Correct |
176 ms |
19132 KB |
Output is correct |
107 |
Correct |
188 ms |
19148 KB |
Output is correct |
108 |
Correct |
282 ms |
28392 KB |
Output is correct |
109 |
Correct |
193 ms |
19356 KB |
Output is correct |
110 |
Correct |
284 ms |
28648 KB |
Output is correct |
111 |
Correct |
211 ms |
18728 KB |
Output is correct |