Submission #529048

#TimeUsernameProblemLanguageResultExecution timeMemory
529048d2k05Power Plant (JOI20_power)C++14
0 / 100
12 ms23808 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 cnt = 0, h = 0; for (auto to : adj[v]) cnt += d[to], h += b[to]; ans = max(ans, (int)a[v - 1]); ans = max(ans, cnt + (h > 0 ? -a[v - 1] : a[v - 1])); for (auto to : adj[v]) { if (to != p) { int dv = d[v], dto = d[to]; int bv = b[v], bto = b[to]; d[v] -= d[to]; if (d[v] <= 1) { if (a[v - 1]) d[v] = b[v] = 1; else { if (d[v] <= 0) b[v] = 0; } } if (d[to] == 1 && a[to - 1] == 1) d[to] = b[to] = 0; d[to] += d[v]; b[to] |= b[v]; d[to] += a[to - 1] * (b[to] > 0 ? -1 : 1); b[to] |= a[to - 1]; if (!d[to]) d[to] = b[to] = a[to - 1]; dfs(to, v); d[v] = dv, d[to] = dto, b[v] = bv, b[to] = bto; } } } 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'; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) d[j] = b[j] = 0; calc(i); ans = max(ans, d[i]); } cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...