Submission #321237

#TimeUsernameProblemLanguageResultExecution timeMemory
321237tushar_2658Power Plant (JOI20_power)C++14
100 / 100
233 ms29856 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); ans = max(ans, dp[x]); } 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; int cnt = 0; for(int i = 1; i <= n; ++i){ a[i] = (s[i - 1] - '0'); cnt += a[i]; } ans = min(2, cnt); 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...