#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
bool generator[200100];
vi G[200100];
int dp[200100][2], n, ans = 0;
void dfs(int at, int p){
dp[at][0] = dp[at][1] = 0;
int all = 0, max1 = 0, max0 = 0;
for(auto next : G[at]){
if(next == p) continue;
dfs(next, at);
all += dp[next][0];
max1 = max(max1, dp[next][1]);
max0 = max(max0, dp[next][0]);
}
if(generator[at]){
dp[at][0] = max(1, all - 1);
dp[at][1] = max0 + 1;
}
else{
dp[at][0] = all;
dp[at][1] = max1;
}
ans = max(ans, dp[at][0]);
ans = max(ans, dp[at][1]);
}
int main(){
cin>>n;
for(int i = 1; i < n; i++){
int a, b;
cin>>a>>b;
a--; b--;
G[a].pb(b);
G[b].pb(a);
}
for(int i = 0; i < n; i++){
char a;
cin>>a;
if(a == '1') generator[i] = true;
else generator[i] = false;
}
dfs(0, -1);
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5012 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5012 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 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 |
5012 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5012 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5012 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 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 |
5012 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
5 ms |
5076 KB |
Output is correct |
24 |
Correct |
5 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5028 KB |
Output is correct |
26 |
Correct |
4 ms |
5072 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
5 ms |
5020 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5092 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5076 KB |
Output is correct |
40 |
Correct |
4 ms |
5152 KB |
Output is correct |
41 |
Correct |
4 ms |
5152 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
5 ms |
5148 KB |
Output is correct |
46 |
Correct |
4 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
5 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5156 KB |
Output is correct |
50 |
Correct |
4 ms |
5152 KB |
Output is correct |
51 |
Correct |
4 ms |
5148 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 |
5076 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
5 ms |
5076 KB |
Output is correct |
57 |
Correct |
6 ms |
5276 KB |
Output is correct |
58 |
Correct |
4 ms |
5028 KB |
Output is correct |
59 |
Correct |
4 ms |
5064 KB |
Output is correct |
60 |
Correct |
4 ms |
5152 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5012 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5012 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 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 |
5012 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
5 ms |
5076 KB |
Output is correct |
24 |
Correct |
5 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5028 KB |
Output is correct |
26 |
Correct |
4 ms |
5072 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
5 ms |
5020 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5092 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5076 KB |
Output is correct |
40 |
Correct |
4 ms |
5152 KB |
Output is correct |
41 |
Correct |
4 ms |
5152 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
4 ms |
5076 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
5 ms |
5148 KB |
Output is correct |
46 |
Correct |
4 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
5 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5156 KB |
Output is correct |
50 |
Correct |
4 ms |
5152 KB |
Output is correct |
51 |
Correct |
4 ms |
5148 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 |
5076 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
5 ms |
5076 KB |
Output is correct |
57 |
Correct |
6 ms |
5276 KB |
Output is correct |
58 |
Correct |
4 ms |
5028 KB |
Output is correct |
59 |
Correct |
4 ms |
5064 KB |
Output is correct |
60 |
Correct |
4 ms |
5152 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
64 |
Correct |
212 ms |
15844 KB |
Output is correct |
65 |
Correct |
220 ms |
15884 KB |
Output is correct |
66 |
Correct |
223 ms |
16076 KB |
Output is correct |
67 |
Correct |
202 ms |
15768 KB |
Output is correct |
68 |
Correct |
203 ms |
15820 KB |
Output is correct |
69 |
Correct |
217 ms |
15816 KB |
Output is correct |
70 |
Correct |
206 ms |
15820 KB |
Output is correct |
71 |
Correct |
225 ms |
15840 KB |
Output is correct |
72 |
Correct |
201 ms |
15800 KB |
Output is correct |
73 |
Correct |
221 ms |
30952 KB |
Output is correct |
74 |
Correct |
244 ms |
30916 KB |
Output is correct |
75 |
Correct |
213 ms |
15724 KB |
Output is correct |
76 |
Correct |
214 ms |
15628 KB |
Output is correct |
77 |
Correct |
211 ms |
15728 KB |
Output is correct |
78 |
Correct |
214 ms |
15640 KB |
Output is correct |
79 |
Correct |
216 ms |
15696 KB |
Output is correct |
80 |
Correct |
243 ms |
15720 KB |
Output is correct |
81 |
Correct |
186 ms |
16088 KB |
Output is correct |
82 |
Correct |
200 ms |
15892 KB |
Output is correct |
83 |
Correct |
212 ms |
21968 KB |
Output is correct |
84 |
Correct |
209 ms |
24476 KB |
Output is correct |
85 |
Correct |
225 ms |
24080 KB |
Output is correct |
86 |
Correct |
234 ms |
17392 KB |
Output is correct |
87 |
Correct |
219 ms |
16784 KB |
Output is correct |
88 |
Correct |
190 ms |
17160 KB |
Output is correct |
89 |
Correct |
205 ms |
17124 KB |
Output is correct |
90 |
Correct |
245 ms |
16916 KB |
Output is correct |
91 |
Correct |
199 ms |
17124 KB |
Output is correct |
92 |
Correct |
200 ms |
17576 KB |
Output is correct |
93 |
Correct |
198 ms |
20424 KB |
Output is correct |
94 |
Correct |
190 ms |
22572 KB |
Output is correct |
95 |
Correct |
203 ms |
21008 KB |
Output is correct |
96 |
Correct |
204 ms |
18824 KB |
Output is correct |
97 |
Correct |
198 ms |
16916 KB |
Output is correct |
98 |
Correct |
188 ms |
16416 KB |
Output is correct |
99 |
Correct |
211 ms |
16108 KB |
Output is correct |
100 |
Correct |
209 ms |
19972 KB |
Output is correct |
101 |
Correct |
210 ms |
17664 KB |
Output is correct |
102 |
Correct |
189 ms |
16588 KB |
Output is correct |
103 |
Correct |
195 ms |
16424 KB |
Output is correct |
104 |
Correct |
154 ms |
16568 KB |
Output is correct |
105 |
Correct |
223 ms |
27072 KB |
Output is correct |
106 |
Correct |
155 ms |
16404 KB |
Output is correct |
107 |
Correct |
154 ms |
16444 KB |
Output is correct |
108 |
Correct |
233 ms |
31840 KB |
Output is correct |
109 |
Correct |
164 ms |
16544 KB |
Output is correct |
110 |
Correct |
224 ms |
32204 KB |
Output is correct |
111 |
Correct |
199 ms |
15720 KB |
Output is correct |