제출 #398477

#제출 시각아이디문제언어결과실행 시간메모리
398477model_codeThe Xana coup (BOI21_xanadu)C++17
100 / 100
143 ms36744 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000000; int N, L[MAXN], dp[MAXN][2][2]; vector<int> e[MAXN]; void dfs(int i = 0, int p = -1) { for (int s = 0; s < 2; s++) { dp[i][s][L[i] ^ s] = s; dp[i][s][L[i] ^ s ^ 1] = N + 1; } for (int j : e[i]) if (j != p) { dfs(j, i); for (int s = 0; s < 2; s++) { int dp0 = dp[i][s][0], dp1 = dp[i][s][1]; dp[i][s][0] = min(N + 1, min(dp0 + dp[j][0][s], dp1 + dp[j][1][s])); dp[i][s][1] = min(N + 1, min(dp0 + dp[j][1][s], dp1 + dp[j][0][s])); } } } int main() { cin >> N; int A, B; for (int i = 0; i < N - 1; i++) { cin >> A >> B; A--, B--; e[A].push_back(B); e[B].push_back(A); } for (int i = 0; i < N; i++) cin >> L[i]; dfs(); int r = min(dp[0][0][0], dp[0][1][0]); if (r > N) cout << "impossible\n"; else cout << r << "\n"; }
#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...