제출 #1093133

#제출 시각아이디문제언어결과실행 시간메모리
1093133juicyThe Xana coup (BOI21_xanadu)C++17
100 / 100
46 ms17176 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif const int N = 1e5 + 5, inf = 1e9; int n; int dp[N][2][2], f[2][2], a[N]; vector<int> g[N]; void dfs(int u, int p) { for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { f[i][j] = dp[u][i][j] = inf; } } dp[u][a[u]][0] = 0; dp[u][a[u] ^ 1][1] = 1; for (int v : g[u]) { if (v ^ p) { dfs(v, u); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { f[j ^ i][0] = min(f[j ^ i][0], dp[u][j][0] + dp[v][0][i]); f[j ^ i][1] = min(f[j ^ i][1], dp[u][j][1] + dp[v][1][i]); } } for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { dp[u][i][j] = f[i][j]; f[i][j] = inf; } } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); 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 = min(dp[1][0][0], dp[1][0][1]); if (res == inf) { cout << "impossible"; } else { cout << res; } 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...