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 target ("sse4.2")
///#pragma GCC optimize("O1")
///#pragma GCC optimize("O2")
///#pragma GCC optimize("O3")
///#pragma GCC optimize("Os")
///#pragma GCC optimize("Ofast")
///#pragma GCC target("avx,avx2,fma")
///#pragma GCC optimization ("unroll-loops")
///#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
#define int ll
const int N = 1e5 + 7;
const int INF = 1e9;
int a[N], dp[N][2][2];
vector<int> g[N];
void dfs(int u, int p = -1) {
int sum[2][2];
sum[0][0] = sum[0][1] = 0;
sum[1][0] = sum[1][1] = INF;
for (auto &it : g[u]) {
if (it != p) {
dfs(it, u);
int old[2][2];
old[0][0] = sum[0][0];
old[1][0] = sum[1][0];
old[0][1] = sum[0][1];
old[1][1] = sum[1][1];
sum[0][0] = min({old[0][0] + dp[it][0][0], old[1][0] + dp[it][1][0], INF});
sum[0][1] = min({old[0][1] + dp[it][0][1], old[1][1] + dp[it][1][1], INF});
sum[1][0] = min({old[0][0] + dp[it][1][0], old[1][0] + dp[it][0][0], INF});
sum[1][1] = min({old[0][1] + dp[it][1][1], old[1][1] + dp[it][0][1], INF});
}
}
if (a[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;
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
dfs(1);
int ans = min(dp[1][0][0], dp[1][1][0]);
if (ans == INF) {
cout << "impossible";
return 0;
}
cout << ans;
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... |