Submission #684037

#TimeUsernameProblemLanguageResultExecution timeMemory
684037Darren0724Power Plant (JOI20_power)C++17
100 / 100
184 ms32388 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...