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;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=1e5+7, INF=1e9+7;
vector<int>V[LIM];
int dp[LIM][2][2], T[LIM];
void DFS(int x, int o) {
int dp2[2][2];
dp2[0][0]=dp2[0][1]=0;
dp2[1][0]=dp2[1][1]=INF;
for(auto i : V[x]) if(i!=o) {
DFS(i, x);
int dp3[2][2];
rep(a, 2) rep(b, 2) dp3[a][b]=INF;
rep(a, 2) {
dp3[0][a]=min(dp3[0][a], dp2[0][a]+dp[i][0][a]);
dp3[0][a]=min(dp3[0][a], dp2[1][a]+dp[i][1][a]);
dp3[1][a]=min(dp3[1][a], dp2[0][a]+dp[i][1][a]);
dp3[1][a]=min(dp3[1][a], dp2[1][a]+dp[i][0][a]);
}
rep(a, 2) rep(b, 2) dp2[a][b]=dp3[a][b];
}
rep(i, 2) {
dp[x][0][i]=min(dp2[i^T[x]][0], INF);
dp[x][1][i]=min(dp2[i^T[x]^1][1]+1, INF);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
rep(i, n-1) {
int a, b;
cin >> a >> b; --a; --b;
V[a].pb(b);
V[b].pb(a);
}
rep(i, n) cin >> T[i];
DFS(0, 0);
if(min(dp[0][0][0], dp[0][1][0])==INF) cout << "impossible\n";
else cout << min(dp[0][0][0], dp[0][1][0]) << '\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... |