제출 #731704

#제출 시각아이디문제언어결과실행 시간메모리
731704NintsiChkhaidzeThe Xana coup (BOI21_xanadu)C++17
100 / 100
125 ms25096 KiB
#include <bits/stdc++.h> #define int long long #define pb push_back using namespace std; const int N = 1e5 + 5,inf=1e9; vector <int> v[N]; int c[N]; int dp[4][N][4]; void dfs(int x,int par){ int valblack = 0,valblack0 = inf,parblack = 1; int valwhite = 0,valwhite0 = inf,parwhite = 0; for (int to: v[x]){ if (to == par) continue; dfs(to,x); //black valblack += min(dp[0][to][1],dp[0][to][0]); if (min(dp[0][to][1],dp[0][to][0]) == dp[0][to][1]) parblack++; valblack0 = min(valblack0, abs(dp[0][to][1] - dp[0][to][0])); //white valwhite += min(dp[1][to][1],dp[1][to][0]); if (min(dp[1][to][1],dp[1][to][0]) == dp[1][to][1]) parwhite++; valwhite0 = min(valwhite0, abs(dp[1][to][1] - dp[1][to][0])); } parblack%=2; parwhite%=2; int color = 1 - c[x]; int oppcolor = c[x]; //x-ზე ოპერაციას ვაკეთებთ if (parblack){ dp[oppcolor][x][1] = min(dp[oppcolor][x][1],valblack + 1); dp[color][x][1] = min(dp[color][x][1],valblack + valblack0 + 1); }else{ dp[color][x][1] = min(dp[color][x][1],valblack + 1); dp[oppcolor][x][1] = min(dp[oppcolor][x][1],valblack + valblack0 + 1); } //x-ზე ოპერაციას არ ვაკეთებთ if (parwhite){ dp[oppcolor][x][0] = min(dp[oppcolor][x][0],valwhite); dp[color][x][0] = min(dp[color][x][0],valwhite + valwhite0); }else{ dp[color][x][0] = min(dp[color][x][0],valwhite); dp[oppcolor][x][0] = min(dp[oppcolor][x][0],valwhite + valwhite0); } } signed main (){ ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL); int n; cin>>n; for (int i = 1; i < n; i++){ int a,b; cin>>a>>b; v[a].pb(b); v[b].pb(a); } for (int i = 1; i <= n; i++) cin>>c[i]; //c[i] = 0 -> i-წვერო თეთრი ფერია for (int i = 1; i <= n; i++) for (int j = 0; j < 2; j++) for (int p = 0; p < 2; p++) dp[p][i][j] = inf; //j ნიშნავს i წვეროზე ოპერაციას ვაკეთებ თუ არა dfs(1,1); int ans = min(dp[1][1][1],dp[1][1][0]); if (ans == inf) cout<<"impossible"; else cout<<ans; }
#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...