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;
int n;
vector<int>g[200005];
string s;
int a[200005];
int dp[200005];
int ans=0;
void dfs(int u,int pa) {
dp[u]=a[u];
int sum1=0,sum2=0;
for(auto v:g[u])
if(v!=pa) {
dfs(v,u);
sum1=max(sum1,dp[v]);
sum2+=dp[v];
}
ans=max(ans,sum1+a[u]);
dp[u]=max(dp[u],sum2-a[u]);
ans=max(ans,dp[u]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1; i<n; i++) {
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
cin>>s;
for(int i=0; i<n; i++)if(s[i]=='1')a[i+1]=1;
dfs(1,0);
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... |