Submission #529053

#TimeUsernameProblemLanguageResultExecution timeMemory
529053d2k05Power Plant (JOI20_power)C++14
100 / 100
188 ms50412 KiB
#include <bits/stdc++.h> #define fastio ios_base :: sync_with_stdio(0), cin.tie(0); using namespace std; using ll = long long; const int mxN = 1e6 + 5, mod = 1e9 + 7; int n, d[mxN], b[mxN], ans; string a; vector <int> adj[mxN]; void calc(int v, int p = -1) { for (auto to : adj[v]) { if (to != p) { calc(to, v); d[v] += d[to]; } } if (d[v] > 0) d[v] -= a[v - 1]; if (!d[v]) d[v] = a[v - 1]; } void dfs(int v, int p = -1) { int ret = 0, cnt = 0; for (auto to : adj[v]) { if (d[to] > 0) ++cnt; ret += d[to]; } if (cnt <= 1) ret += a[v - 1]; else { if (ret > 0) ret -= a[v - 1]; if (!ret) ret = a[v - 1]; } ans = max(ans, ret); for (auto to : adj[v]) { if (to != p) { int dv = d[v], dto = d[to]; d[v] -= d[to]; if (d[v] <= 1) { if (a[v - 1]) d[v] = 1; } if (d[to] == 1 && a[to - 1] == 1) d[to] = 0; d[to] += d[v]; if (d[to] > 0) d[to] -= a[to - 1]; if (!d[to]) d[to] = a[to - 1]; dfs(to, v); d[v] = dv, d[to] = dto; } } } int main() { fastio; cin >> n; for (int i = 1, u, v; i < n; ++i) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } cin >> a; for (auto &x : a) x -= '0'; calc(1); dfs(1); cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...