This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |