#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<vector<int>> v;
vector<ll> dp;
string s;
ll ans = 0;
void dfs(int x, int p){
ll su = 0, mx = 0;
for (auto el : v[x]){
if (el != p){
dfs(el, x);
su += dp[el];
mx = max(dp[el], mx);
}
}
if (s[x] == '0'){
dp[x] = su;
ans = max(su, ans);
}
else{
dp[x] = max(1LL, su - 1);
ans = max(ans, max(mx + 1, su - 1));
}
}
int main(){
int n, a, b;
cin>>n;
v.resize(n + 1);
dp.assign(n + 1, 0);
for (int i = 0; i < n - 1; i++){
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
cin>>s;
s = " " + s;
dfs(1, 0);
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
444 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
444 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
436 KB |
Output is correct |
22 |
Correct |
2 ms |
424 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
532 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
2 ms |
420 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
600 KB |
Output is correct |
45 |
Correct |
1 ms |
440 KB |
Output is correct |
46 |
Correct |
2 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
584 KB |
Output is correct |
48 |
Correct |
2 ms |
600 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
496 KB |
Output is correct |
51 |
Correct |
2 ms |
432 KB |
Output is correct |
52 |
Correct |
2 ms |
544 KB |
Output is correct |
53 |
Correct |
2 ms |
612 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
2 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
2 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
604 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
444 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
436 KB |
Output is correct |
22 |
Correct |
2 ms |
424 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
532 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
2 ms |
420 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
600 KB |
Output is correct |
45 |
Correct |
1 ms |
440 KB |
Output is correct |
46 |
Correct |
2 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
584 KB |
Output is correct |
48 |
Correct |
2 ms |
600 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
496 KB |
Output is correct |
51 |
Correct |
2 ms |
432 KB |
Output is correct |
52 |
Correct |
2 ms |
544 KB |
Output is correct |
53 |
Correct |
2 ms |
612 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
2 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
2 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
604 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
133 ms |
16100 KB |
Output is correct |
65 |
Correct |
114 ms |
16096 KB |
Output is correct |
66 |
Correct |
116 ms |
16080 KB |
Output is correct |
67 |
Correct |
109 ms |
16132 KB |
Output is correct |
68 |
Correct |
148 ms |
16060 KB |
Output is correct |
69 |
Correct |
137 ms |
16096 KB |
Output is correct |
70 |
Correct |
127 ms |
16096 KB |
Output is correct |
71 |
Correct |
111 ms |
16088 KB |
Output is correct |
72 |
Correct |
135 ms |
16104 KB |
Output is correct |
73 |
Correct |
127 ms |
28480 KB |
Output is correct |
74 |
Correct |
138 ms |
28444 KB |
Output is correct |
75 |
Correct |
131 ms |
16096 KB |
Output is correct |
76 |
Correct |
121 ms |
16056 KB |
Output is correct |
77 |
Correct |
125 ms |
16032 KB |
Output is correct |
78 |
Correct |
174 ms |
16100 KB |
Output is correct |
79 |
Correct |
134 ms |
16068 KB |
Output is correct |
80 |
Correct |
106 ms |
16104 KB |
Output is correct |
81 |
Correct |
120 ms |
16328 KB |
Output is correct |
82 |
Correct |
129 ms |
16076 KB |
Output is correct |
83 |
Correct |
126 ms |
21028 KB |
Output is correct |
84 |
Correct |
120 ms |
23100 KB |
Output is correct |
85 |
Correct |
117 ms |
22844 KB |
Output is correct |
86 |
Correct |
110 ms |
17436 KB |
Output is correct |
87 |
Correct |
106 ms |
16668 KB |
Output is correct |
88 |
Correct |
105 ms |
17184 KB |
Output is correct |
89 |
Correct |
119 ms |
17212 KB |
Output is correct |
90 |
Correct |
107 ms |
16920 KB |
Output is correct |
91 |
Correct |
108 ms |
17184 KB |
Output is correct |
92 |
Correct |
109 ms |
17540 KB |
Output is correct |
93 |
Correct |
103 ms |
19864 KB |
Output is correct |
94 |
Correct |
128 ms |
21708 KB |
Output is correct |
95 |
Correct |
115 ms |
20376 KB |
Output is correct |
96 |
Correct |
104 ms |
18652 KB |
Output is correct |
97 |
Correct |
102 ms |
16924 KB |
Output is correct |
98 |
Correct |
103 ms |
16628 KB |
Output is correct |
99 |
Correct |
107 ms |
16612 KB |
Output is correct |
100 |
Correct |
133 ms |
19512 KB |
Output is correct |
101 |
Correct |
109 ms |
17512 KB |
Output is correct |
102 |
Correct |
98 ms |
16676 KB |
Output is correct |
103 |
Correct |
104 ms |
16692 KB |
Output is correct |
104 |
Correct |
105 ms |
16936 KB |
Output is correct |
105 |
Correct |
139 ms |
25372 KB |
Output is correct |
106 |
Correct |
90 ms |
16708 KB |
Output is correct |
107 |
Correct |
96 ms |
16744 KB |
Output is correct |
108 |
Correct |
122 ms |
29468 KB |
Output is correct |
109 |
Correct |
88 ms |
16840 KB |
Output is correct |
110 |
Correct |
119 ms |
29580 KB |
Output is correct |
111 |
Correct |
102 ms |
15940 KB |
Output is correct |