Submission #626783

#TimeUsernameProblemLanguageResultExecution timeMemory
626783codr0Power Plant (JOI20_power)C++14
100 / 100
131 ms30488 KiB
// 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...