제출 #557965

#제출 시각아이디문제언어결과실행 시간메모리
557965AdamGSThe Xana coup (BOI21_xanadu)C++17
100 / 100
75 ms18424 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const int LIM=1e5+7, INF=1e9+7; vector<int>V[LIM]; int dp[LIM][2][2], T[LIM]; void DFS(int x, int o) { int dp2[2][2]; dp2[0][0]=dp2[0][1]=0; dp2[1][0]=dp2[1][1]=INF; for(auto i : V[x]) if(i!=o) { DFS(i, x); int dp3[2][2]; rep(a, 2) rep(b, 2) dp3[a][b]=INF; rep(a, 2) { dp3[0][a]=min(dp3[0][a], dp2[0][a]+dp[i][0][a]); dp3[0][a]=min(dp3[0][a], dp2[1][a]+dp[i][1][a]); dp3[1][a]=min(dp3[1][a], dp2[0][a]+dp[i][1][a]); dp3[1][a]=min(dp3[1][a], dp2[1][a]+dp[i][0][a]); } rep(a, 2) rep(b, 2) dp2[a][b]=dp3[a][b]; } rep(i, 2) { dp[x][0][i]=min(dp2[i^T[x]][0], INF); dp[x][1][i]=min(dp2[i^T[x]^1][1]+1, INF); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i, n-1) { int a, b; cin >> a >> b; --a; --b; V[a].pb(b); V[b].pb(a); } rep(i, n) cin >> T[i]; DFS(0, 0); if(min(dp[0][0][0], dp[0][1][0])==INF) cout << "impossible\n"; else cout << min(dp[0][0][0], dp[0][1][0]) << '\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...