Submission #715329

#TimeUsernameProblemLanguageResultExecution timeMemory
715329stevancvThe Xana coup (BOI21_xanadu)C++14
100 / 100
86 ms16920 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define sp ' ' #define en '\n' #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) using namespace std; const int N = 1e5 + 2; const int inf = 1e9; int dp[N][2][2], a[N]; vector<int> g[N]; void Dfs(int s, int e) { for (int j = 0; j < 4; j++) dp[s][j / 2][j % 2] = inf; if (g[s].size() == 1 && g[s][0] == e) { dp[s][a[s]][0] = 0; dp[s][1 - a[s]][1] = 1; return; } for (int u : g[s]) if (u != e) Dfs(u, s); int cnt, sum, mn, moze; cnt = sum = 0; mn = inf; moze = 1; for (int u : g[s]) { if (u == e) continue; sum += min(dp[u][0][0], dp[u][0][1]); if (min(dp[u][0][0], dp[u][0][1]) == inf) moze = 0; smin(mn, abs(dp[u][0][0] - dp[u][0][1])); if (dp[u][0][0] > dp[u][0][1]) cnt += 1; } if (moze == 1) { dp[s][0][0] = sum; if (mn <= 1000000) dp[s][1][0] = sum + mn; if ((cnt + a[s]) % 2 == 1) swap(dp[s][0][0], dp[s][1][0]); } cnt = sum = 0; mn = inf; moze = 1; for (int u : g[s]) { if (u == e) continue; sum += min(dp[u][1][0], dp[u][1][1]); if (min(dp[u][1][0], dp[u][1][1]) == inf) moze = 0; smin(mn, abs(dp[u][1][0] - dp[u][1][1])); if (dp[u][1][0] > dp[u][1][1]) cnt += 1; } if (moze == 1) { dp[s][0][1] = sum + 1; if (mn <= 1000000) dp[s][1][1] = sum + mn + 1; if ((cnt + a[s]) % 2 == 0) swap(dp[s][0][1], dp[s][1][1]); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++) cin >> a[i]; Dfs(1, 0); int ans = min(dp[1][0][0], dp[1][0][1]); if (ans == inf) cout << "impossible" << en; else cout << ans << en; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...