#include using namespace std; #define rep(i, n) for (int i=0; i G[200010]; int c[200010]; int memo[2][200010]; int dfs(int f, int v, int pv=-1) { if (memo[f][v]!=-1) return memo[f][v]; int dp0 = 0; int dp1 = 1e9; for (int nv : G[v]) { if (nv==pv) continue; int c0 = dfs(0, nv, v); int c1 = dfs(1, nv, v); int ndp0 = min(dp0+c1, dp1+c0+1); int ndp1 = min(dp0+c0+1, dp1+c1); dp0 = ndp0; dp1 = ndp1; } int res = c[v]^f ? dp1 : dp0; return memo[f][v] = res; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; rep(i, N-1) { int u, v; cin >> u >> v; G[u-1].pb(v-1); G[v-1].pb(u-1); } rep(i, N) cin >> c[i]; rep(f, 2) rep(v, N) memo[f][v] = -1; int ans = dfs(1, 0); if (ans>=1e9) cout << -1 << endl; else cout << ans << endl; }