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;
const int maxn=200000+10;
int dp[maxn],res=0,n,val[maxn];
vector<int>adj[maxn];
void dfs(int u=1,int par=0){
for(auto x:adj[u]){
if(x!=par){
dfs(x,u);
dp[u]+=dp[x];
res=max(res,dp[x]+val[u]);
}
}
dp[u]=max(dp[u]+val[u]*-1,val[u]);
return ;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=1;i<=n;i++)
{
char c;
cin>>c;
val[i]=c-'0';
}
dfs(1);
res=max(res,dp[1]);
cout<<res<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |