Submission #481893

#TimeUsernameProblemLanguageResultExecution timeMemory
481893radalThe Xana coup (BOI21_xanadu)C++14
100 / 100
86 ms22796 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O2") #pragma GCC target("avx2,fma") #define rep(i,l,r) for (int i = l; i < r; i++) #define repr(i,r,l) for (int i = r; i >= l; i--) #define X first #define Y second #define pb push_back #define endl '\n' #define debug(x) cerr << #x << " : " << x << endl; using namespace std; typedef long long ll; typedef pair<int,int> pll; const int N = 3e5+20,mod = 1e9+7,inf = 2e9,sq = 400; int poww(int n,ll k){ if (!k) return 1; if (k == 1) return n; int r = poww(n,k/2); return 1ll*r*r%mod*poww(n,k&1)%mod; } inline int mkay(int x,int y){ if (x+y < mod) return x+y; return x+y-mod; } ll dp[N][2][2],dp2[N][2][2]; bool on[N]; vector<int> adj[N]; void dfs(int v,int p){ int sz = adj[v].size(); rep(i,0,sz){ int u = adj[v][i]; if (u != p){ dfs(u,v); } } rep(i,0,sz){ int u = adj[v][i]; if (u != p){ if (!i){ rep(j,0,2) rep(k,0,2) dp2[i][j][k] = dp[u][j][k]; continue; } dp2[i][0][0] = min(dp2[i-1][0][0]+dp[u][0][0],dp2[i-1][1][0]+dp[u][1][0]); dp2[i][0][1] = min(dp2[i-1][0][1]+dp[u][0][1],dp2[i-1][1][1]+dp[u][1][1]); dp2[i][1][0] = min(dp2[i-1][0][0]+dp[u][1][0],dp2[i-1][1][0]+dp[u][0][0]); dp2[i][1][1] = min(dp2[i-1][0][1]+dp[u][1][1],dp2[i-1][1][1]+dp[u][0][1]); } else{ if (i){ rep(j,0,2) rep(k,0,2) dp2[i][j][k] = dp2[i-1][j][k]; continue; } rep(j,0,2) rep(k,0,2) dp2[i][j][k] = inf; dp2[i][0][1] = 0; dp2[i][0][0] = 0; } } if (on[v]){ dp[v][0][0] = dp2[sz-1][1][0]; dp[v][0][1] = dp2[sz-1][0][0]; dp[v][1][0] = dp2[sz-1][0][1]+1; dp[v][1][1] = dp2[sz-1][1][1]+1; } else{ dp[v][0][0] = dp2[sz-1][0][0]; dp[v][0][1] = dp2[sz-1][1][0]; dp[v][1][0] = dp2[sz-1][1][1]+1; dp[v][1][1] = dp2[sz-1][0][1]+1; } } int main(){ ios :: sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i,0,n-1){ int u,v; cin >> u >> v; adj[v].pb(u); adj[u].pb(v); } rep(i,1,n+1) cin >> on[i]; dfs(1,-1); ll ans; ans = min(dp[1][0][0],dp[1][1][0]); if (ans >= inf){ cout << "impossible"; return 0; } 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...