제출 #525907

#제출 시각아이디문제언어결과실행 시간메모리
525907QingyuThe Xana coup (BOI21_xanadu)C++17
100 / 100
108 ms56928 KiB
#include <bits/stdc++.h> const int N = 1e6 + 50; std::vector<int> G[N]; int a[N], dp[N][2][2], g[N][2][2], h[2][2]; void dfs(int x, int fa = 0) { memset(g[x], 0x3f, sizeof g[x]); g[x][0][0] = g[x][1][0] = 0; for (int y : G[x]) if (y != fa) { dfs(y, x); memcpy(h, g[x], sizeof h); for (int o : {0, 1}) for (int p : {0, 1}) g[x][o][p] = std::min(0x3f3f3f3f, std::min(h[o][p] + dp[y][0][o], h[o][!p] + dp[y][1][o])); } for (int o : {0, 1}) for (int p : {0, 1}) dp[x][o][a[x] ^ o ^ p] = g[x][o][p] + o; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int n; std::cin >> n; for (int i = 1; i < n; ++i) { int x, y; std::cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } for (int i = 1; i <= n; ++i) std::cin >> a[i]; memset(dp, 0x3f, sizeof dp); dfs(1); int ans = std::min(dp[1][0][0], dp[1][1][0]); if (ans > n) { puts("impossible"); } else { printf("%d\n", ans); } 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...