Submission #577603

#TimeUsernameProblemLanguageResultExecution timeMemory
577603jiahngThe Xana coup (BOI21_xanadu)C++14
100 / 100
126 ms25036 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define int ll typedef pair<int32_t, int32_t> pi; typedef vector <int> vi; typedef vector <pi> vpi; typedef pair<pi, ll> pii; typedef set <ll> si; typedef long double ld; #define f first #define s second #define mp make_pair #define FOR(i,s,e) for(int i=s;i<=int(e);++i) #define DEC(i,s,e) for(int i=s;i>=int(e);--i) #define pb push_back #define all(x) (x).begin(), (x).end() #define lbd(x, y) lower_bound(all(x), y) #define ubd(x, y) upper_bound(all(x), y) #define aFOR(i,x) for (auto i: x) #define mem(x,i) memset(x,i,sizeof x) #define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define maxn 100010 #define INF (ll)1e9 #define MOD 1000000007 typedef pair <vi, int> pvi; typedef pair <int,pi> ipi; typedef vector <pii> vpii; int N,A[maxn]; vi adj[maxn]; int a,b; int dp[maxn][2][2]; int dpf(int i,int j,int k,int p){ // i is the node, j is current state and k is whether node is selected if (dp[i][j][k] != -1) return dp[i][j][k]; vi v; int cur = 0; aFOR(x,adj[i]) if (x != p){ v.pb(dpf(x,k^A[x]^1,1,i)+1 - dpf(x,k^A[x],0,i)); cur += dpf(x,k^A[x],0,i); } sort(all(v)); int ptr = 0; if (j){ if (v.empty()) return dp[i][j][k] = INF; cur += v[ptr]; ptr++; } dp[i][j][k] = cur; while (ptr + 1 < (int)v.size()){ cur += v[ptr] + v[ptr+1]; ptr += 2; dp[i][j][k] = min(dp[i][j][k], cur); } assert(dp[i][j][k] >= 0); // cout << i << ' ' << j << ' ' << dp[i][j][k] << '\n'; return dp[i][j][k]; } int32_t main(){ fast; cin >> N; FOR(i,1,N-1){ cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } mem(dp,-1); FOR(i,1,N) cin >> A[i]; int ans = min(dpf(1,A[1],0,-1), dpf(1,!A[1],1,-1) + 1); 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...