Submission #1089048

#TimeUsernameProblemLanguageResultExecution timeMemory
1089048VMaksimoski008The Xana coup (BOI21_xanadu)C++17
100 / 100
127 ms16980 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; vector<int> graph[maxn]; int n, vec[maxn], dp[maxn][2][2]; void dfs(int u, int p) { dp[u][vec[u]][0] = 0; dp[u][vec[u]^1][1] = 1; for(int &v : graph[u]) { if(v == p) continue; dfs(v, u); int tmp[2][2]; tmp[0][0] = tmp[0][1] = tmp[1][0] = tmp[1][1] = 1e9; for(int i=0; i<2; i++) for(int j=0; j<2; j++) for(int k=0; k<2; k++) tmp[i^j][k] = min(tmp[i^j][k], dp[u][i][k] + dp[v][k][j]); for(int i=0; i<2; i++) for(int j=0; j<2; j++) dp[u][i][j] = tmp[i][j]; } } int main() { cin >> n; for(int i=0; i<n-1; i++) { int a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } for(int i=1; i<=n; i++) cin >> vec[i]; for(int i=1; i<=n; i++) dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = 1e9; dfs(1, 1); int x = min(dp[1][0][0], dp[1][0][1]); if(x == 1e9) cout << "impossible"; else cout << x; 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...