This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 100000;
const int INF = INT_MAX / 2;
int N;
vector <int> adj[N_MAX + 2];
bool on[N_MAX + 2];
int dp[N_MAX + 2][2][2];
void dfs (int u, int parent = 0) {
int sum[2][2];
sum[0][0] = sum[0][1] = 0;
sum[1][0] = sum[1][1] = INF;
for (int v : adj[u]) {
if (v != parent) {
dfs(v, u);
int old[2][2];
old[0][0] = sum[0][0]; old[0][1] = sum[0][1];
old[1][0] = sum[1][0]; old[1][1] = sum[1][1];
sum[0][0] = min({old[0][0] + dp[v][0][0], old[1][0] + dp[v][1][0], INF});
sum[0][1] = min({old[0][1] + dp[v][0][1], old[1][1] + dp[v][1][1], INF});
sum[1][0] = min({old[0][0] + dp[v][1][0], old[1][0] + dp[v][0][0], INF});
sum[1][1] = min({old[0][1] + dp[v][1][1], old[1][1] + dp[v][0][1], INF});
}
}
if (on[u] == 1) {
dp[u][0][0] = sum[1][0];
dp[u][0][1] = sum[0][0];
dp[u][1][0] = sum[0][1] + 1;
dp[u][1][1] = sum[1][1] + 1;
} else {
dp[u][0][0] = sum[0][0];
dp[u][0][1] = sum[1][0];
dp[u][1][0] = sum[1][1] + 1;
dp[u][1][1] = sum[0][1] + 1;
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= N - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int u = 1; u <= N; u++) {
cin >> on[u];
}
dfs(1);
int answer = min(dp[1][0][0], dp[1][1][0]);
if (answer <= N) {
cout << answer << "\n";
} else {
cout << "impossible\n";
}
return 0;
}
# | 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... |