Submission #312694

#TimeUsernameProblemLanguageResultExecution timeMemory
312694Jarif_RahmanPower Plant (JOI20_power)C++17
0 / 100
0 ms384 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; int n; str s; vector <vector<int>> v; vector <ll> sth, root; ll ans = 0; ll dfs1(int nd, int ss){ ll r = '0'-s[nd]; if(v[nd].size() == 1 && s[nd] == '1') r = 1; for(int x: v[nd]) if(x != ss) r+=dfs1(x, nd); sth[nd] = max(r, (ll)s[nd] - '0'); return sth[nd]; } void dfs2(int nd, int ss){ ll sss = sth[nd]; if(ss != -1){ root[ss]+=sth[nd]; sss+=max(sth[ss] - sth[nd], (ll)s[ss] - '0'); } sth[nd] = sss; for(int x: v[nd]) if(ss != x){ sss-=sth[x]; root[x]+=max(sss, (ll)s[nd] - '0'); sss+=sth[x]; } for(int x: v[nd]) if(x != ss) dfs2(x, nd); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; v = vector<vector<int>> (n+1); sth = vector <ll> (n+1, -1); root = vector<ll> (n+1, 0); for(int i = 1; i < n; i++){ int a, b; cin >> a >> b; v[a].pb(b); v[b].pb(a); } cin >> s; s = "x"+s; dfs1(1, -1); dfs2(1, -1); for(int i = 1; i <+ n; i++){ if(v[i].size() == 1){ root[i] = max(root[i]+s[i]-'0', (ll)s[i] - '0'); } else root[i] = max(root[i]+'0'-s[i], (ll)s[i] - '0'); } for(int i = 1; i <= n; i++) ans = max(ans, root[i]); cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...