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 <iostream>
#include <vector>
using namespace std;
#define FOR(i,n) for(int i = 1; i <= N; i++)
vector<int> edge[100001];
int sv[100001];
vector<int> p(100001, 0);
vector<int> children[100001];
int** dp[100001];
void dfs1(int u) {
for(int v: edge[u]) {
if(p[u] == v) continue;
p[v] = u;
children[u].push_back(v);
dfs1(v);
}
}
int** X;
void dfs2(int u) {
dp[u][ sv[u] ][0] = dp[u][ sv[u] ][1] = 0;
for(int v: children[u]) {
dfs2(v);
for(int f = 0; f <= 1; f++) for(int g = 0; g <= 1; g++) X[f][g] = min(dp[u][f][g] + dp[v][g][0], dp[u][!f][g] + dp[v][!g][1] + 1);
swap(dp[u], X);
}
}
int main() {
X = new int*[2];
for(int f = 0; f < 2; f++) X[f] = new int[2];
int N; cin >> N;
for(int i = 1; i <= N-1; i++) {
int a, b; cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
edge[1].push_back(0);
edge[0].push_back(1);
dfs1(0);
for(int i = 0; i <= N; i++) {
dp[i] = new int*[2];
for(int f = 0; f <= 1; f++)
{
dp[i][f] = new int[2];
for(int g = 0; g <= 1; g++) dp[i][f][g] = N+1;
}
}
FOR(i, N) cin >> sv[i];
dp[0][0][0] = dp[0][0][1] = dp[0][1][0] = dp[0][1][1] = 0;
dfs2(0);
int res = min(dp[0][0][0], dp[0][1][0]);
if(res >= N+1) cout << "impossible\n";
else cout << res << '\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... |