# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
444249 | parsabahrami | The Xana coup (BOI21_xanadu) | C++17 | 139 ms | 36644 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.
/* I do it for the glory */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 1e6 + 10, MOD = 1e9 + 7;
int n, C[N]; vector<int> adj[N]; ll dp[2][2][N]; // [root ro zadim ya na][root akharesh chie][root] :clown:
void DFS(int v, int p = -1) {
if (SZ(adj[v]) < 2 && v > 1) {
dp[0][C[v]][v] = 0;
dp[0][C[v] ^ 1][v] = dp[1][C[v]][v] = MOD;
dp[1][!C[v]][v] = 1;
return;
}
for (int &u : adj[v])
if (u != p) DFS(u, v);
ll c = 0, m = MOD;
for (int &u : adj[v]) {
if (u != p) {
if (dp[1][0][u] <= dp[0][0][u])
c++;
m = min(m, abs(dp[1][0][u] - dp[0][0][u]));
}
}
for (int &u : adj[v]) {
if (u != p) {
dp[0][(c & 1) ^ C[v]][v] += min(dp[1][0][u], dp[0][0][u]);
}
}
dp[0][(c & 1) ^ C[v] ^ 1][v] = dp[0][(c & 1) ^ C[v]][v] + m;
c = 0, m = MOD;
for (int &u : adj[v]) {
if (u != p) {
if (dp[1][1][u] <= dp[0][1][u])
c++;
m = min(m, abs(dp[1][1][u] - dp[0][1][u]));
}
}
for (int &u : adj[v]) {
if (u != p) {
dp[1][(c & 1) ^ C[v] ^ 1][v] += min(dp[1][1][u], dp[0][1][u]);
}
}
dp[1][(c & 1) ^ C[v] ^ 1][v]++;
dp[1][(c & 1) ^ C[v]][v] = dp[1][(c & 1) ^ C[v] ^ 1][v] + m;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++)
scanf("%d", &C[i]);
DFS(1);
printf(min(dp[1][0][1], dp[0][0][1]) > n ? "impossible\n" : "%lld\n", min(dp[1][0][1], dp[0][0][1]));
return 0;
}
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... |