Submission #766279

#TimeUsernameProblemLanguageResultExecution timeMemory
766279yusuf12360Power Plant (JOI20_power)C++14
100 / 100
134 ms31620 KiB
#include<bits/stdc++.h> using namespace std; int ans=1; vector<int> res; vector<vector<int>> adj; string s; void dfs(int u=0, int p=-1) { int sum=0, mx=0, path=0; for(int pp : adj[u]) { if(pp==p) continue; dfs(pp, u); if(res[pp]>0) sum+=res[pp], mx=max(mx, res[pp]), path++;} res[u]=sum; if(path==0) {res[u]=s[u]-'0'; return;} if(s[u]=='1') res[u]--, ans=max({ans, sum-2*(path>1)+1, mx+1}); else ans=max(ans, sum); res[u]=max(1, res[u]); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; res.resize(n); adj.resize(n); while(--n) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u);} cin >> s; dfs(); cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...