#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
//#define endl '\n'
int n;
vector<int> adj[200001];
int it[200001];
int dp[200001][2];
int ans=0;
void dfs(int no,int par=-1){
for(auto j:adj[no]){
if(j!=par){
dfs(j,no);
}
}
dp[no][1]=0;
int su=0;
if(it[no]){
dp[no][1]=1;
dp[no][0]=1;
su-=1;
}
for(auto j:adj[no]){
if(j!=par){
su+=dp[j][1];
}
}
dp[no][1]=max(dp[no][1],su);
int ma=0;
int ma2=0;
for(auto j:adj[no]){
if(j!=par){
ma=max(ma,dp[j][1]);
ma2=max(ma2,dp[j][0]);
}
}
if(it[no]){
ma+=1;
}
dp[no][0]=max(dp[no][0],ma);
dp[no][0]=max(dp[no][0],ma2);
int su2=0;
if(it[no]){
su2-=1;
}
for(auto j:adj[no]){
if(j!=par){
su2+=dp[j][1];
}
}
dp[no][0]=max(dp[no][0],su2);
ans=max(ans,max(dp[no][0],dp[no][1]));
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(int i=0;i<n-1;i++){
int aa,bb;
cin>>aa>>bb;
aa--;
bb--;
adj[aa].pb(bb);
adj[bb].pb(aa);
}
for(int i=0;i<n;i++){
char s;
cin>>s;
if(s=='0'){
it[i]=0;
}
else{
it[i]=1;
}
}
dfs(0);
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
5060 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
5060 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
5120 KB |
Output is correct |
28 |
Correct |
4 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5248 KB |
Output is correct |
31 |
Correct |
4 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5120 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5120 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
5 ms |
5120 KB |
Output is correct |
45 |
Correct |
4 ms |
5120 KB |
Output is correct |
46 |
Correct |
4 ms |
5120 KB |
Output is correct |
47 |
Correct |
5 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5120 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5120 KB |
Output is correct |
54 |
Correct |
4 ms |
5120 KB |
Output is correct |
55 |
Correct |
4 ms |
5120 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
6 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
5 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5248 KB |
Output is correct |
61 |
Correct |
5 ms |
5120 KB |
Output is correct |
62 |
Correct |
4 ms |
5248 KB |
Output is correct |
63 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
5060 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
5120 KB |
Output is correct |
28 |
Correct |
4 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5248 KB |
Output is correct |
31 |
Correct |
4 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
4 ms |
5120 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5120 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
5 ms |
5120 KB |
Output is correct |
45 |
Correct |
4 ms |
5120 KB |
Output is correct |
46 |
Correct |
4 ms |
5120 KB |
Output is correct |
47 |
Correct |
5 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5120 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5120 KB |
Output is correct |
54 |
Correct |
4 ms |
5120 KB |
Output is correct |
55 |
Correct |
4 ms |
5120 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
6 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
5 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5248 KB |
Output is correct |
61 |
Correct |
5 ms |
5120 KB |
Output is correct |
62 |
Correct |
4 ms |
5248 KB |
Output is correct |
63 |
Correct |
5 ms |
5120 KB |
Output is correct |
64 |
Correct |
187 ms |
13688 KB |
Output is correct |
65 |
Correct |
187 ms |
13816 KB |
Output is correct |
66 |
Correct |
191 ms |
13816 KB |
Output is correct |
67 |
Correct |
180 ms |
13688 KB |
Output is correct |
68 |
Correct |
180 ms |
13816 KB |
Output is correct |
69 |
Correct |
180 ms |
13820 KB |
Output is correct |
70 |
Correct |
193 ms |
13820 KB |
Output is correct |
71 |
Correct |
186 ms |
13816 KB |
Output is correct |
72 |
Correct |
191 ms |
13688 KB |
Output is correct |
73 |
Correct |
226 ms |
26364 KB |
Output is correct |
74 |
Correct |
228 ms |
26364 KB |
Output is correct |
75 |
Correct |
189 ms |
13688 KB |
Output is correct |
76 |
Correct |
192 ms |
13688 KB |
Output is correct |
77 |
Correct |
205 ms |
13688 KB |
Output is correct |
78 |
Correct |
203 ms |
13688 KB |
Output is correct |
79 |
Correct |
207 ms |
13688 KB |
Output is correct |
80 |
Correct |
203 ms |
13688 KB |
Output is correct |
81 |
Correct |
179 ms |
14032 KB |
Output is correct |
82 |
Correct |
203 ms |
13808 KB |
Output is correct |
83 |
Correct |
214 ms |
18880 KB |
Output is correct |
84 |
Correct |
216 ms |
20984 KB |
Output is correct |
85 |
Correct |
233 ms |
20728 KB |
Output is correct |
86 |
Correct |
219 ms |
15096 KB |
Output is correct |
87 |
Correct |
208 ms |
14456 KB |
Output is correct |
88 |
Correct |
201 ms |
14972 KB |
Output is correct |
89 |
Correct |
200 ms |
14968 KB |
Output is correct |
90 |
Correct |
208 ms |
14712 KB |
Output is correct |
91 |
Correct |
192 ms |
14952 KB |
Output is correct |
92 |
Correct |
197 ms |
15352 KB |
Output is correct |
93 |
Correct |
195 ms |
17780 KB |
Output is correct |
94 |
Correct |
190 ms |
19440 KB |
Output is correct |
95 |
Correct |
192 ms |
18080 KB |
Output is correct |
96 |
Correct |
211 ms |
16288 KB |
Output is correct |
97 |
Correct |
189 ms |
14792 KB |
Output is correct |
98 |
Correct |
182 ms |
14328 KB |
Output is correct |
99 |
Correct |
193 ms |
14200 KB |
Output is correct |
100 |
Correct |
194 ms |
17272 KB |
Output is correct |
101 |
Correct |
196 ms |
15280 KB |
Output is correct |
102 |
Correct |
181 ms |
14712 KB |
Output is correct |
103 |
Correct |
193 ms |
14328 KB |
Output is correct |
104 |
Correct |
117 ms |
14392 KB |
Output is correct |
105 |
Correct |
231 ms |
23328 KB |
Output is correct |
106 |
Correct |
135 ms |
14492 KB |
Output is correct |
107 |
Correct |
132 ms |
14388 KB |
Output is correct |
108 |
Correct |
236 ms |
27120 KB |
Output is correct |
109 |
Correct |
125 ms |
14440 KB |
Output is correct |
110 |
Correct |
226 ms |
27424 KB |
Output is correct |
111 |
Correct |
191 ms |
13688 KB |
Output is correct |