Submission #1025065

#TimeUsernameProblemLanguageResultExecution timeMemory
1025065phoenixThe Xana coup (BOI21_xanadu)C++17
100 / 100
42 ms17004 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 100100; const int INF = 1e9; int n; vector<int> g[N]; int a[N]; int dp[N][2][2]; void dfs(int s, int p) { for (int to : g[s]) if (to != p) { dfs(to, s); } for (int x = 0; x <= 1; x++) { int f[2]{0, INF}; for (int to : g[s]) if (to != p) { bool t = (x ^ a[to]); int g[2]{INF, INF}; for (int xc = 0; xc <= 1; xc++) { for (int yc = 0; yc <= 1; yc++) { if ((xc ^ yc) != (t)) continue; for (int pr = 0; pr <= 1; pr++) { g[(pr ^ xc)] = min(g[(pr ^ xc)], f[pr] + dp[to][xc][yc]); } } } f[0] = g[0]; f[1] = g[1]; } dp[s][x][0] = f[0] + x; dp[s][x][1] = f[1] + x; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n; 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, 1); int res = INF; for (int x = 0; x <= 1; x++) for (int y = 0; y <= 1; y++) if ((x ^ y) == a[1]) res = min(res, dp[1][x][y]); if (res == INF) { cout << "impossible\n"; return 0; } else { cout << res << '\n'; 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...