#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=2e5+5;
vi P[MAX];
int c[MAX];
int dp[MAX];
char s[MAX];
int ans=0;
void dfs(int u,int fa){
vi d;
d.clear();
vi wrzut;
for (auto it:P[u]){
if (it==fa)continue;
dfs(it,u);
d.pb(it);
wrzut.pb(dp[it]);
}
if (c[u]==0){
for (auto v:d)dp[u]+=dp[v];
ans=max(ans,dp[u]);
}
else{
assert(c[u]==1);
int sum=0;
for (auto v:d)sum+=dp[v];
if (sum==0)dp[u]=1,ans=max(ans,dp[u]);
else{
dp[u]=max(1LL,sum-1);
sort(wrzut.begin(),wrzut.end());
if (sz(wrzut)){
ans=max(ans,wrzut.back()+1);
}
ans=max(ans,dp[u]);
}
}
}
int32_t main(){
BOOST;
int n;
cin>>n;
for (int i=1;i<=n-1;i++){
int a,b;
cin>>a>>b;
P[a].pb(b);
P[b].pb(a);
}
for (int i=1;i<=n;i++){
cin>>s[i];
c[i]=s[i]-'0';
}
dfs(1,-1);
for (int i=1;i<=n;i++)cerr<<"DPEK "<<i<<" "<<dp[i]<<"\n";
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5012 KB |
Output is correct |
2 |
Correct |
4 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
5020 KB |
Output is correct |
9 |
Correct |
3 ms |
5012 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
5 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5012 KB |
Output is correct |
2 |
Correct |
4 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
5020 KB |
Output is correct |
9 |
Correct |
3 ms |
5012 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
5 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
15 ms |
5176 KB |
Output is correct |
23 |
Correct |
16 ms |
5068 KB |
Output is correct |
24 |
Correct |
15 ms |
5076 KB |
Output is correct |
25 |
Correct |
16 ms |
5168 KB |
Output is correct |
26 |
Correct |
18 ms |
5068 KB |
Output is correct |
27 |
Correct |
14 ms |
5068 KB |
Output is correct |
28 |
Correct |
14 ms |
5188 KB |
Output is correct |
29 |
Correct |
17 ms |
5324 KB |
Output is correct |
30 |
Correct |
20 ms |
5324 KB |
Output is correct |
31 |
Correct |
17 ms |
5192 KB |
Output is correct |
32 |
Correct |
17 ms |
5068 KB |
Output is correct |
33 |
Correct |
20 ms |
5128 KB |
Output is correct |
34 |
Correct |
15 ms |
5148 KB |
Output is correct |
35 |
Correct |
15 ms |
5164 KB |
Output is correct |
36 |
Correct |
15 ms |
5068 KB |
Output is correct |
37 |
Correct |
14 ms |
5196 KB |
Output is correct |
38 |
Correct |
17 ms |
5160 KB |
Output is correct |
39 |
Correct |
15 ms |
5280 KB |
Output is correct |
40 |
Correct |
24 ms |
5248 KB |
Output is correct |
41 |
Correct |
17 ms |
5280 KB |
Output is correct |
42 |
Correct |
17 ms |
5248 KB |
Output is correct |
43 |
Correct |
18 ms |
5216 KB |
Output is correct |
44 |
Correct |
17 ms |
5208 KB |
Output is correct |
45 |
Correct |
16 ms |
5328 KB |
Output is correct |
46 |
Correct |
17 ms |
5216 KB |
Output is correct |
47 |
Correct |
14 ms |
5196 KB |
Output is correct |
48 |
Correct |
16 ms |
5324 KB |
Output is correct |
49 |
Correct |
16 ms |
5356 KB |
Output is correct |
50 |
Correct |
23 ms |
5196 KB |
Output is correct |
51 |
Correct |
16 ms |
5236 KB |
Output is correct |
52 |
Correct |
19 ms |
5180 KB |
Output is correct |
53 |
Correct |
15 ms |
5244 KB |
Output is correct |
54 |
Correct |
14 ms |
5152 KB |
Output is correct |
55 |
Correct |
15 ms |
5156 KB |
Output is correct |
56 |
Correct |
16 ms |
5224 KB |
Output is correct |
57 |
Correct |
15 ms |
5432 KB |
Output is correct |
58 |
Correct |
16 ms |
5248 KB |
Output is correct |
59 |
Correct |
16 ms |
5236 KB |
Output is correct |
60 |
Correct |
17 ms |
5324 KB |
Output is correct |
61 |
Correct |
16 ms |
5236 KB |
Output is correct |
62 |
Correct |
17 ms |
5324 KB |
Output is correct |
63 |
Correct |
20 ms |
5156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5012 KB |
Output is correct |
2 |
Correct |
4 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
5020 KB |
Output is correct |
9 |
Correct |
3 ms |
5012 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
5 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
15 ms |
5176 KB |
Output is correct |
23 |
Correct |
16 ms |
5068 KB |
Output is correct |
24 |
Correct |
15 ms |
5076 KB |
Output is correct |
25 |
Correct |
16 ms |
5168 KB |
Output is correct |
26 |
Correct |
18 ms |
5068 KB |
Output is correct |
27 |
Correct |
14 ms |
5068 KB |
Output is correct |
28 |
Correct |
14 ms |
5188 KB |
Output is correct |
29 |
Correct |
17 ms |
5324 KB |
Output is correct |
30 |
Correct |
20 ms |
5324 KB |
Output is correct |
31 |
Correct |
17 ms |
5192 KB |
Output is correct |
32 |
Correct |
17 ms |
5068 KB |
Output is correct |
33 |
Correct |
20 ms |
5128 KB |
Output is correct |
34 |
Correct |
15 ms |
5148 KB |
Output is correct |
35 |
Correct |
15 ms |
5164 KB |
Output is correct |
36 |
Correct |
15 ms |
5068 KB |
Output is correct |
37 |
Correct |
14 ms |
5196 KB |
Output is correct |
38 |
Correct |
17 ms |
5160 KB |
Output is correct |
39 |
Correct |
15 ms |
5280 KB |
Output is correct |
40 |
Correct |
24 ms |
5248 KB |
Output is correct |
41 |
Correct |
17 ms |
5280 KB |
Output is correct |
42 |
Correct |
17 ms |
5248 KB |
Output is correct |
43 |
Correct |
18 ms |
5216 KB |
Output is correct |
44 |
Correct |
17 ms |
5208 KB |
Output is correct |
45 |
Correct |
16 ms |
5328 KB |
Output is correct |
46 |
Correct |
17 ms |
5216 KB |
Output is correct |
47 |
Correct |
14 ms |
5196 KB |
Output is correct |
48 |
Correct |
16 ms |
5324 KB |
Output is correct |
49 |
Correct |
16 ms |
5356 KB |
Output is correct |
50 |
Correct |
23 ms |
5196 KB |
Output is correct |
51 |
Correct |
16 ms |
5236 KB |
Output is correct |
52 |
Correct |
19 ms |
5180 KB |
Output is correct |
53 |
Correct |
15 ms |
5244 KB |
Output is correct |
54 |
Correct |
14 ms |
5152 KB |
Output is correct |
55 |
Correct |
15 ms |
5156 KB |
Output is correct |
56 |
Correct |
16 ms |
5224 KB |
Output is correct |
57 |
Correct |
15 ms |
5432 KB |
Output is correct |
58 |
Correct |
16 ms |
5248 KB |
Output is correct |
59 |
Correct |
16 ms |
5236 KB |
Output is correct |
60 |
Correct |
17 ms |
5324 KB |
Output is correct |
61 |
Correct |
16 ms |
5236 KB |
Output is correct |
62 |
Correct |
17 ms |
5324 KB |
Output is correct |
63 |
Correct |
20 ms |
5156 KB |
Output is correct |
64 |
Correct |
1393 ms |
21144 KB |
Output is correct |
65 |
Correct |
1368 ms |
21232 KB |
Output is correct |
66 |
Correct |
1402 ms |
21068 KB |
Output is correct |
67 |
Correct |
1390 ms |
21156 KB |
Output is correct |
68 |
Correct |
1342 ms |
21092 KB |
Output is correct |
69 |
Correct |
1361 ms |
21152 KB |
Output is correct |
70 |
Correct |
1414 ms |
21100 KB |
Output is correct |
71 |
Correct |
1356 ms |
21128 KB |
Output is correct |
72 |
Correct |
1355 ms |
21324 KB |
Output is correct |
73 |
Correct |
1349 ms |
45380 KB |
Output is correct |
74 |
Correct |
1425 ms |
45236 KB |
Output is correct |
75 |
Correct |
1336 ms |
22348 KB |
Output is correct |
76 |
Correct |
1395 ms |
22256 KB |
Output is correct |
77 |
Correct |
1482 ms |
20980 KB |
Output is correct |
78 |
Correct |
1455 ms |
21072 KB |
Output is correct |
79 |
Correct |
1473 ms |
21080 KB |
Output is correct |
80 |
Correct |
1333 ms |
21060 KB |
Output is correct |
81 |
Correct |
1369 ms |
21336 KB |
Output is correct |
82 |
Correct |
1474 ms |
21368 KB |
Output is correct |
83 |
Correct |
1408 ms |
32188 KB |
Output is correct |
84 |
Execution timed out |
1502 ms |
36960 KB |
Time limit exceeded |
85 |
Halted |
0 ms |
0 KB |
- |