Submission #415159

#TimeUsernameProblemLanguageResultExecution timeMemory
415159achibasadzishviliThe Xana coup (BOI21_xanadu)C++17
100 / 100
135 ms20400 KiB
#include<bits/stdc++.h> #define ll long long #define f first #define s second #define pb push_back using namespace std; ll n,c[100005]; vector<ll>v[100005]; ll dp[100005][2][2]; void solve(ll x,ll par){ ll keon = 1e9,luon = 0,keof = 1e9,luof = 0; bool ye = 0; for(auto to : v[x]){ if(to == par)continue; solve(to , x); ye = 1; ll wk = keon,wl = luon; keon = min(wk + dp[to][0][1] , wl + dp[to][1][1]); luon = min(wl + dp[to][0][1] , wk + dp[to][1][1]); wk = keof , wl = luof; keof = min(wk + dp[to][0][0] , wl + dp[to][1][0]); luof = min(wl + dp[to][0][0] , wk + dp[to][1][0]); } for(int i=0; i<2; i++) for(int j=0; j<2; j++) dp[x][i][j] = 1e9; if(!ye){ dp[x][0][c[x]] = 0; dp[x][1][c[x] ^ 1] = 1; return; } dp[x][0][1 ^ c[x]] = min(dp[x][0][1 ^ c[x]] , keof); dp[x][0][c[x]] = min(dp[x][0][c[x]] , luof); dp[x][1][c[x]] = min(dp[x][1][c[x]] , keon + 1); dp[x][1][c[x] ^ 1] = min(dp[x][1][c[x] ^ 1] , luon + 1); } int main(){ ios::sync_with_stdio(false); cin >> n; for(int i=1; i<n; i++){ ll x,y; cin >> x >> y; v[x].pb(y); v[y].pb(x); } for(int i=1; i<=n; i++){ cin >> c[i]; } solve(1 , 0); ll ans = min(dp[1][1][0] , dp[1][0][0]); if(ans > 1e8) cout << "impossible\n"; else cout << ans << '\n'; 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...