This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Code by Parsa Eslami
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)x.size())
#define bit(i,j) ((j>>i)&1)
#define pb push_back
#define lc 2*id
#define rc 2*id+1
#define dmid int mid=(r+l)/2
#define err(x) cerr<#x<<" : "<<x<<'\n'
using namespace std;
const int N=2e5+4;
vector<int> adj[N];
int gen[N];
int dp[N][2];
int in(){
int x; cin>>x; return x;
}
void dfs(int v,int p){
int sg1=0,mx1=0,mx0=0;
for(int u:adj[v]) if(u!=p){
dfs(u,v);
sg1+=dp[u][1];
maxm(mx1,dp[u][1]);
maxm(mx0,dp[u][0]);
}
maxm(dp[v][1],gen[v]);
maxm(dp[v][1],sg1-gen[v]);
maxm(dp[v][0],mx0);
maxm(dp[v][0],mx1+gen[v]);
maxm(dp[v][0],sg1-gen[v]);
}
int32_t main(){
iostream::sync_with_stdio(0); cin.tie(0);
int n=in();
FOR(i,1,n-1){
int u=in(),v=in();
adj[u].pb(v);
adj[v].pb(u);
}
string s; cin>>s;
FOR(i,0,n-1) gen[i+1]=s[i]-'0';
dfs(1,1);
cout<<dp[1][0]<<'\n';
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... |