제출 #560227

#제출 시각아이디문제언어결과실행 시간메모리
560227Ooops_sorryThe Xana coup (BOI21_xanadu)C++14
100 / 100
125 ms34180 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define ld long double #define ll long long mt19937 rnd(51); const int N = 1e5 + 10, INF = 1e9; vector<int> g[N], a(N); int dp[N][2][2]; void dfs(int v, int p) { vector<int> dp0(2), dp1(2); dp0[1] = dp1[1] = INF; for (auto to : g[v]) { if (to != p) { dfs(to, v); vector<int> dpp0(2, INF), dpp1(2, INF); for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { dpp0[j ^ k] = min(dpp0[j ^ k], dp0[j] + dp[to][0][k]); dpp1[j ^ k] = min(dpp1[j ^ k], dp1[j] + dp[to][1][k]); } } swap(dp0, dpp0); swap(dp1, dpp1); } } dp[v][a[v]][0] = min(dp[v][a[v]][0], dp0[0]); dp[v][a[v] ^ 1][0] = min(dp[v][a[v] ^ 1][0], dp0[1]); dp[v][a[v] ^ 1][1] = min(dp[v][a[v] ^ 1][1], dp1[0] + 1); dp[v][a[v]][1] = min(dp[v][a[v]][1], dp1[1] + 1); } signed main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif // LCOAL ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { dp[i][j][k] = INF; } } } for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--, b--; g[a].pb(b); g[b].pb(a); } for (int i = 0; i < n; i++) cin >> a[i]; dfs(0, -1); int res = min(dp[0][0][1], dp[0][0][0]); if (res == INF) cout << "impossible" << endl; else cout << res << endl; 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...