Submission #1129562

#TimeUsernameProblemLanguageResultExecution timeMemory
1129562NewtonabcPower Plant (JOI20_power)C++20
100 / 100
224 ms31936 KiB
#include<bits/stdc++.h> using namespace std; const int N=2e5+10; vector<int> adj[N]; int dp[N][2]; string s; void dfs(int u,int p=-1){ int mx=0; dp[u][0]=0; dp[u][1]=s[u]-'0'; for(int v:adj[u]){ if(v==p) continue; dfs(v,u); dp[u][0]+=max({dp[v][1]-(s[v]-'0')*2,dp[v][0]}); dp[u][1]=max({dp[u][1],dp[v][0]+s[u]-'0',dp[v][1]-1}); } dp[u][0]=max(s[u]-'0',dp[u][0]-(s[u]-'0')); if(s[u]-'0'==0) dp[u][1]=0; } int main(){ int n; cin>>n; for(int i=0;i<n-1;i++){ int a,b; cin>>a >>b; adj[a].push_back(b); adj[b].push_back(a); } cin>>s; s=" "+s; int root; for(int i=1;i<=n;i++){ if(s[i]=='1'){ dfs(i); root=i; break; } } int ans=0; for(int i=1;i<=n;i++){ ans=max({ans,dp[i][0],dp[i][1]}); } cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...