Submission #297787

#TimeUsernameProblemLanguageResultExecution timeMemory
297787eriksuenderhaufPower Plant (JOI20_power)C++17
100 / 100
245 ms25704 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; vector<int> adj[N]; int dp[N], rsm[N], sm[N], val[N]; int dfs(int u, int p = -1) { dp[u] = -val[u]; // don't cut for (int v: adj[u]) if (v != p) sm[u] += dfs(v, u); return dp[u] = max(-dp[u], dp[u] + sm[u]); } void reroot(int u, int p = -1) { rsm[u] = sm[u] + (~p ? max(val[p], -val[p] + rsm[p] - dp[u]) : 0); dp[u] = max(val[u], rsm[u] - val[u]); 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); } string s; cin >> s; int ans = 0; for (int i = 0; i < n; i++) ans += val[i] = (s[i] == '1'); ans = min(ans, 2); 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...