Submission #296148

#TimeUsernameProblemLanguageResultExecution timeMemory
296148eriksuenderhaufPower Plant (JOI20_power)C++17
0 / 100
5 ms4992 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; vector<int> adj[N]; int dp[N], rsm[N], sm[N]; string s; void dfs(int u, int p = -1) { dp[u] = -(s[u] == '1'); // don't cut sm[u] = 0; for (int v: adj[u]) if (v != p) { dfs(v, u); sm[u] += dp[v]; } dp[u] = max(-dp[u], dp[u] + sm[u]); } void reroot(int u, int p = -1) { if (~p) { int vp = s[p] == '1' ? 1 : 0; rsm[u] = sm[u] + max(vp, -vp + rsm[p] - dp[u]); } else { rsm[u] = sm[u]; } int vu = s[u] == '1' ? 1 : 0; dp[u] = max(vu, rsm[u] - vu); for (int v: adj[u]) if (v != p) { reroot(v, u); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1, u, v; i < n; i++) { cin >> u >> v; u--, v--; adj[u].push_back(v); adj[v].push_back(u); } cin >> s; int ans = 0; dfs(0); reroot(0); for (int i = 0; i < n; i++) ans = max(ans, dp[i]); cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...