제출 #525223

#제출 시각아이디문제언어결과실행 시간메모리
525223pokmui9909The Xana coup (BOI21_xanadu)C++17
100 / 100
56 ms25168 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll INF = 1e9 + 7; ll N; vector<ll> G[100005]; ll C[100005]; ll D[100005][2][2]; void dfs(ll u, ll p){ D[u][0][C[u]] = 0, D[u][1][C[u] ^ 1] = 1; D[u][0][C[u] ^ 1] = D[u][1][C[u]] = INF; for(auto v : G[u]){ if(v == p) continue; dfs(v, u); ll T[2][2]; for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ T[i][j] = INF; for(int k = 0; k < 2; k++){ T[i][j] = min(T[i][j], D[u][i][j ^ k] + D[v][k][i]); } } } for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ D[u][i][j] = T[i][j]; } } } } int main(){ cin.tie(0) -> sync_with_stdio(false); cin >> N; for(int i = 1; i < N; i++){ ll u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } for(int i = 1; i <= N; i++){ cin >> C[i]; } dfs(1, -1); ll r = min(D[1][0][0], D[1][1][0]); if(r <= N){ cout << r << '\n'; } else { cout << "impossible\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...