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;
#define ll long long
#define vt vector
#define ar array
map<int, vt<int>> lines;
int L[1000005];
int dp[1000005][2][2];
int n;
void dfs(int cur, int prev){
for(int s = 0; s < 2; s++){
dp[cur][s][L[cur] ^ s] = s;
dp[cur][s][L[cur] ^ s ^ 1] = n + 1;
}
for (int j : lines[cur]){
if (j != prev){
dfs(j, cur);
for (int s = 0; s < 2; s++){
int dp0 = dp[cur][s][0];
int dp1 = dp[cur][s][1];
dp[cur][s][0] = min(n + 1, min(dp0 + dp[j][0][s], dp1 + dp[j][1][s]));
dp[cur][s][1] = min(n + 1, min(dp0 + dp[j][1][s], dp1 + dp[j][0][s]));
}
}
}
}
void solve(){
cin >> n;
for (int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
a--; b--;
if (lines.find(a) == lines.end()) lines[a] = {};
if (lines.find(b) == lines.end()) lines[b] = {};
lines[a].push_back(b);
lines[b].push_back(a);
}
for(int i = 0; i < n; i++){
cin >> L[i];
}
dfs(0, -1);
int res = min(dp[0][0][0], dp[0][1][0]);
if (res > n) cout << "impossible" << endl;
else cout << res << endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int testcases = 1;
// cin >> testcases;
while(testcases--){
solve();
}
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... |