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 f first
#define s second
#define all(x) x.begin(),x.end();
#define sz(x) (int)x.size()
int n, sta[100005];
int dp[100005][2][2];
vector<int> v[100005];
void dfs(int nw, int fa)
{
    int min_swap_all_child_on = 1e9, min_swap_all_child_off = 1e9;
    int min_all_child_on = 0, min_all_child_off = 0;
    int min_all_child_on_click = 0, min_all_child_off_click = 0;
    bool not_on = false, not_off = false;
    bool swap_on = false, swap_off = false;
    for(auto go: v[nw])
    {
        if(go != fa)
        {
            dfs(go, nw);
            if(dp[go][1][0] == 1e9 && dp[go][1][1] == 1e9)
                not_on = true;
            if(dp[go][1][0] <= dp[go][1][1])
                min_all_child_on += dp[go][1][0];
            else
                min_all_child_on += dp[go][1][1], min_all_child_on_click++;
            if(max(dp[go][1][0], dp[go][1][1]) != 1e9)
            {
                swap_on = true;
                min_swap_all_child_on = min(min_swap_all_child_on, abs(dp[go][1][1] - dp[go][1][0]));
            }
            if(dp[go][0][0] == 1e9 && dp[go][0][1] == 1e9)
                not_off = true;
            if(dp[go][0][0] <= dp[go][0][1])
                min_all_child_off += dp[go][0][0];
            else
                min_all_child_off += dp[go][0][1], min_all_child_off_click++;
            if(max(dp[go][0][0], dp[go][0][1]) != 1e9)
            {
                swap_off = true;
                min_swap_all_child_off = min(min_swap_all_child_off, abs(dp[go][0][0] - dp[go][0][1]));
            }
        }
    }
    if(sz(v[nw]) == 1 && v[nw][0] == fa) // leaf
    {
        // not click
        dp[nw][sta[nw]][0] = 0;
        // click
        dp[nw][sta[nw] ^ 1][1] = 1;
    }
    else
    {
        int cur_sta;
        if(!not_on) // all child on
        {
            // need click
            cur_sta = sta[nw] ^ (min_all_child_on_click % 2) ^ 1;
            dp[nw][cur_sta][1] = min_all_child_on + 1;
            if(swap_on)
                dp[nw][cur_sta ^ 1][1] = min_all_child_on + min_swap_all_child_on + 1;
        }
        if(!not_off) // all child off
        {
            // need not click
            cur_sta = sta[nw] ^ (min_all_child_off_click % 2);
            dp[nw][cur_sta][0] = min_all_child_off;
            if(swap_off)
                dp[nw][cur_sta ^ 1][0] = min_all_child_off + min_swap_all_child_off;
        }
    }
}
int main()
{
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
        dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = 1e9;
    for(int i = 0; i < n - 1; i++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        v[a].pb(b);
        v[b].pb(a);
    }
    for(int i = 1; i <= n; i++)
        scanf("%d",&sta[i]);
    dfs(1, 0);
    int ans = min(dp[1][0][0], dp[1][0][1]);
    if(ans == 1e9)
        printf("impossible\n");
    else
        printf("%d\n",ans);
    return 0;
}
Compilation message (stderr)
xanadu.cpp: In function 'int main()':
xanadu.cpp:88:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
xanadu.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |         scanf("%d %d",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~
xanadu.cpp:99:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         scanf("%d",&sta[i]);
      |         ~~~~~^~~~~~~~~~~~~~| # | 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... |