Submission #321235

#TimeUsernameProblemLanguageResultExecution timeMemory
321235tushar_2658Power Plant (JOI20_power)C++14
0 / 100
4 ms5228 KiB
#include "bits/stdc++.h" using namespace std; const int maxn = 200005; vector<int> edges[maxn]; int a[maxn], dp[maxn], ans = 0; void dfs(int x, int p){ int cur = a[x], res = 0; dp[x] = cur; for(auto i : edges[x]){ if(i != p){ dfs(i, x); res += dp[i]; } } res -= cur; dp[x] = max(dp[x], res); } void dfs1(int x, int p, int carry){ int res = carry - a[x]; for(auto i : edges[x]){ if(i != p){ res += dp[i]; } } ans = max(ans, res); for(auto i : edges[x]){ if(i != p){ dfs1(i, x, max(a[x], res - dp[i])); } } } int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i = 0; i < n - 1; ++i){ int x, y; cin >> x >> y; edges[x].push_back(y); edges[y].push_back(x); } string s; cin >> s; for(int i = 1; i <= n; ++i){ a[i] = (s[i - 1] - '0'); } dfs(1, 1); dfs1(1, 1, 0); cout << ans << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...