Submission #1301712

#TimeUsernameProblemLanguageResultExecution timeMemory
1301712efegThe Xana coup (BOI21_xanadu)C++20
100 / 100
77 ms29736 KiB
#include <bits/stdc++.h> using namespace std; #define sz(v) (int) v.size() #define pb push_back #define popcount(x) __builtin_popcount(x) using i64 = long long; template<typename T> using vec = vector<T>; template<typename T> void chmin(T &a,T b){ a = min(a,b); } int n; vec<vec<int>> adj; vec<int> col; i64 dp[100100][2][2]; void dfs(int node,int p){ for (auto x : adj[node]){ if (x == p) continue; dfs(x,node); } vec<vec<i64>> currdp(2,vec<i64>(2,INT_MAX)); currdp[col[node]][0] = 0; currdp[(col[node] +1) % 2][1] = 1; for (auto x : adj[node]){ if (x == p) continue; vec<vec<i64>> nxtdp(2,vec<i64>(2,INT_MAX)); for (int j = 0; j < 2; j++){ for (int k = 0; k < 2; k++){ chmin(nxtdp[j][k],currdp[j][k] + dp[x][k][0]); chmin(nxtdp[j][k],currdp[(j+1) % 2][k] + dp[x][k][1]); } } swap(currdp,nxtdp); } for (int j = 0; j < 2; j++){ for (int k = 0; k < 2; k++) dp[node][j][k] = currdp[j][k]; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; adj.assign(n,vec<int>()); col.assign(n,0); for (int i = 0; i < n-1; i++){ int u,v; cin >> u >> v; u--; v--; adj[u].pb(v); adj[v].pb(u); } for (int i = 0; i < n; i++) cin >> col[i]; for (int i = 0; i < n; i++){ for (int j = 0; j < 2; j++){ for (int k = 0; k < 2 ; k++){ dp[i][j][k] = INT_MAX; } } } dfs(0,-1); // for (int i = 0; i < n; i++){ // for (int j = 0; j < 2; j++){ // for (int k = 0; k < 2; k++) cout << i << " " << j <<" " << k << ": " << dp[i][j][k] << endl; // } // } i64 ans = min(dp[0][0][0],dp[0][0][1]); if (ans == INT_MAX) cout << "impossible" << endl; else cout << ans << endl; 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...