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>
using namespace std;
const int N = 100'000 + 10;
int n;
vector<int> ad[N];
int a[N];
long long f[N][2][2], dp[2][2];
void dfs(int u, int p = -1) {
f[u][0][0] = f[u][0][1] = f[u][1][0] = f[u][1][1] = N;
f[u][a[u]][0] = 0;
f[u][a[u] ^ 1][1] = 1;
for (const auto& v : ad[u]) {
if (v == p) continue;
dfs(v, u);
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) dp[i][j] = f[u][i][j];
}
for (int a = 0; a < 2; ++a) {
for (int b = 0; b < 2; ++b) {
auto& ret = f[u][a][b];
ret = min(dp[a][b] + f[v][b][0], dp[a ^ 1][b] + f[v][b][1]);
}
}
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 2; i <= n; ++i) {
int u, v; cin >> u >> v;
ad[u].push_back(v);
ad[v].push_back(u);
}
for (int i = 1; i <= n; ++i) cin >> a[i];
dfs(1);
// cerr << f[5][0][1] << "\n";
int answer = min(f[1][0][0], f[1][0][1]);
if (answer > n) cout << "imposible\n";
else cout << answer << "\n";
}
# | 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... |