Submission #647156

#TimeUsernameProblemLanguageResultExecution timeMemory
647156Ronin13The Xana coup (BOI21_xanadu)C++14
100 / 100
141 ms16928 KiB
#include<bits/stdc++.h> #define ll long long #define f first #define s second #define pii pair<int,int> #define pll pair<ll,ll> #define pb push_back #define epb emplace_back #define ull unsigned ll using namespace std; const int NMAX = 1e5 + 1; const int inf = 1e9 + 1; int dp[NMAX][2][2]; int a[NMAX]; vector <vector <int> > g(NMAX); void dfs(int v, int e = -1){ int ind = 0; for(int to : g[v]){ if(to == e) continue; dfs(to, v); ind++; } if(!ind){ dp[v][a[v]][0] = 0; dp[v][1 ^ a[v]][1] = 1; return; } for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ int mn = inf; int cnt = 0; int sum = 0; for(int to : g[v]){ if(to == e) continue; if(dp[to][y][0] > dp[to][y][1]) sum += dp[to][y][1], cnt++, mn = min(mn, dp[to][y][0] - dp[to][y][1]); else sum += dp[to][y][0], mn = min(dp[to][y][1] - dp[to][y][0], mn); sum = min(sum, inf); } if((cnt + y + a[v]) % 2 == x) dp[v][x][y] = min(dp[v][x][y], sum + y); else dp[v][x][y] = min(dp[v][x][y], sum + mn + y); } } } int main(){ int n; cin >> n; for(int i = 1; i <= n; i++){ for(int x = 0; x < 2; x++){ for(int y = 0; y < 2;y++) dp[i][x][y] = inf; } } for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } for(int i = 1; i <= n; i++) cin >> a[i]; dfs(1); /*for(int i= 1; i <= n; i++){ for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++) cout << dp[i][x][y] << ' '; } cout << "\n"; }*/ int v = min(dp[1][0][1], dp[1][0][0]); if(v == inf){ cout << "impossible\n"; return 0; } cout << v; 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...