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 pb push_back
#define ld long double
#define ll long long
mt19937 rnd(51);
const int N = 1e5 + 10, INF = 1e9;
vector<int> g[N], a(N);
int dp[N][2][2];
void dfs(int v, int p) {
    vector<int> dp0(2), dp1(2);
    dp0[1] = dp1[1] = INF;
    for (auto to : g[v]) {
        if (to != p) {
            dfs(to, v);
            vector<int> dpp0(2, INF), dpp1(2, INF);
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    dpp0[j ^ k] = min(dpp0[j ^ k], dp0[j] + dp[to][0][k]);
                    dpp1[j ^ k] = min(dpp1[j ^ k], dp1[j] + dp[to][1][k]);
                }
            }
            swap(dp0, dpp0);
            swap(dp1, dpp1);
        }
    }
    dp[v][a[v]][0] = min(dp[v][a[v]][0], dp0[0]);
    dp[v][a[v] ^ 1][0] = min(dp[v][a[v] ^ 1][0], dp0[1]);
    dp[v][a[v] ^ 1][1] = min(dp[v][a[v] ^ 1][1], dp1[0] + 1);
    dp[v][a[v]][1] = min(dp[v][a[v]][1], dp1[1] + 1);
}
signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif // LCOAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                dp[i][j][k] = INF;
            }
        }
    }
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        g[a].pb(b);
        g[b].pb(a);
    }
    for (int i = 0; i < n; i++) cin >> a[i];
    dfs(0, -1);
    int res = min(dp[0][0][1], dp[0][0][0]);
    if (res == INF) cout << "impossible" << endl;
    else cout << res << endl;
    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... |