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 maxn = 1e5 + 5;
vector<int> graph[maxn];
int n, vec[maxn], dp[maxn][2][2];
void dfs(int u, int p) {
dp[u][vec[u]][0] = 0;
dp[u][vec[u]^1][1] = 1;
for(int &v : graph[u]) {
if(v == p) continue;
dfs(v, u);
int tmp[2][2];
tmp[0][0] = tmp[0][1] = tmp[1][0] = tmp[1][1] = 1e9;
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
for(int k=0; k<2; k++) tmp[i^j][k] = min(tmp[i^j][k], dp[u][i][k] + dp[v][k][j]);
for(int i=0; i<2; i++)
for(int j=0; j<2; j++) dp[u][i][j] = tmp[i][j];
}
}
int main() {
cin >> n;
for(int i=0; i<n-1; i++) {
int a, b; cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for(int i=1; i<=n; i++) cin >> vec[i];
for(int i=1; i<=n; i++) dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = 1e9;
dfs(1, 1);
int x = min(dp[1][0][0], dp[1][0][1]);
if(x == 1e9) cout << "impossible";
else cout << x;
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... |