# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
828928 | QwertyPi | The Xana coup (BOI21_xanadu) | C++14 | 96 ms | 18860 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#define int long long
using namespace std;
const int N = 1e5 + 11;
const int INF = 1 << 30;
int dp[N][2][2];
vector<int> G[N];
int a[N];
int chmin(int& x, int y){
x = min(x, y); return x;
}
void dfs(int v, int pa = -1){
int ch = 0;
int d[2][2] = {{INF, INF}, {INF, INF}};
for(auto u : G[v]){
if(u == pa) continue; ch++; dfs(u, v);
}
for(int x = 0; x < 2; x++){
int b[2][2] = {{0, INF}, {INF, INF}};
for(auto u : G[v]){
if(u == pa) continue;
for(int z = 0; z < 2; z++){
for(int y = 0; y < 2; y++){
chmin(b[1][(z + y) % 2], b[0][z] + dp[u][x][y]);
}
}
for(int y = 0; y < 2; y++) swap(b[0][y], b[1][y]), b[1][y] = INF;
}
for(int y = 0; y < 2; y++){
dp[v][a[v] ^ y ^ x][x] = b[0][y] + x;
}
}
if(ch == 0){
dp[v][a[v]][0] = 0;
dp[v][!a[v]][1] = 1;
return;
}
}
int32_t main(){
int n; cin >> n;
for(int i = 0; i < n - 1; i++){
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
for(int i = 1; i <= n; i++){
cin >> a[i];
}
memset(dp, 0x3f, sizeof(dp));
dfs(1);
int ans = min(dp[1][0][0], dp[1][0][1]);
cout << (ans > N ? "impossible" : to_string(ans)) << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |