#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define fi first
#define se second
#define ld long double
#define pb push_back
int N;
string c;
vector<int> v[200010],p[200010];
void dfs(int pos,int prec,int good){
if(good!=-1 && c[pos]=='1'){
p[good].pb(pos);
}
if(c[pos]=='1')good=pos;
for(int x:v[pos]){
if(x==prec)continue;
dfs(x,pos,good);
}
}
int ans(int pos,int prec){
vector<int> k={0};
int tot=0;
for(int x:v[pos]){
if(x==prec)continue;
k.pb(ans(x,pos));
tot+=k.back();
}
sort(k.begin(),k.end(),greater<int> ());
int ma;
if(c[pos]=='1'){
if(k[0]<=1)k[0]++;
ma=max(k[0],tot-1);
}else{
ma=tot;
}
//cout<<pos<<" "<<ma<<endl;
return ma;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);
cin>>N;
for(int i=0;i<N-1;i++){
int a,b;
cin>>a>>b;
a--;b--;
v[a].pb(b);
v[b].pb(a);
}
cin>>c;
cout<<ans(0,-1)<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9748 KB |
Output is correct |
2 |
Correct |
8 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9624 KB |
Output is correct |
5 |
Incorrect |
7 ms |
9720 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9748 KB |
Output is correct |
2 |
Correct |
8 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9624 KB |
Output is correct |
5 |
Incorrect |
7 ms |
9720 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9748 KB |
Output is correct |
2 |
Correct |
8 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9624 KB |
Output is correct |
5 |
Incorrect |
7 ms |
9720 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |