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;
#define int long long
#define all(x) x.begin(),x.end()
int INF=1e18;
int n;
vector<vector<int>> adj;
vector<int> v;
vector<int> dp;
vector<int> dp1;
void dfs(int k,int pa){
dp[k]=v[k];
int total=0;
int mx=0;
for(int j:adj[k]){
if(j==pa)continue;
dfs(j,k);
total+=dp[j];
mx=max(mx,dp[j]);
}
dp[k]=max(dp[k],total-v[k]);
dp1[k]=max(dp[k],mx+v[k]);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
adj.resize(n);
v.resize(n);
dp.resize(n);
dp1.resize(n);
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;a--;b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i=0;i<n;i++){
char c;cin>>c;
v[i]=c-'0';
}
dfs(0,0);
int mx=0;
for(int i=0;i<n;i++){
mx=max(mx,dp[i]);
mx=max(mx,dp1[i]);
}
cout<<mx<<endl;
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... |