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>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pi;
typedef vector<pi> pii;
typedef tuple<int,int,int> tii;
typedef vector<ll> li;
typedef vector<li> lii;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define LSOne(s) ((s)&(-s))
ll INF=1000000000000000010;
int inf=1e9+10;
ll M=1e9+7;
int n;
vii a;
vi p,dp;
int ans=0;
void dfs(int x){
int k=0;
for(auto u:a[x]){
if(p[x]==u)continue;
p[u]=x;
dfs(u);
k+=dp[u];
}
if(dp[x]){
if(x==0&&a[x].size()==1){
ans=max(ans,dp[x]+k);
dp[x]=max(dp[x],k-1);
return;
}
else if(x!=0&&a[x].size()==2){
ans=max(ans,dp[x]+k);
dp[x]=max(dp[x],k-1);
return;
}
ans=max(ans,dp[x]+k-1);
dp[x]=max(dp[x],k-1);
return;
}
dp[x]=max(dp[x],k);
}
int main() {
cin>>n;
a.resize(n);p.resize(n,-1);dp.resize(n,0);
REP(i,0,n-1){
int x,y;cin>>x>>y;
x--;y--;
a[x].PB(y);
a[y].PB(x);
}
REP(i,0,n){
char x;cin>>x;
if(x-'0'==1)dp[i]=1;
}
dfs(0);
cout<<max(ans,dp[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... |