#include #include #include #include using namespace std; int main(){ int n; cin >> n; vector> G(n); for(int i = 0; i < n-1; i++){ int a, b; cin >> a >> b; a--; b--; G[a].emplace_back(b); G[b].emplace_back(a); } vector c(n); for(auto &it: c) cin >> it; int blackcnt = 0; for(auto &it: c) blackcnt += it; if(blackcnt%2 != n%2){ cout << -1 << endl; return 0; } vector in(n); queue Q; for(int i = 0; i < n; i++){ in[i] = G[i].size(); if(in[i] == 1) Q.emplace(i); } int ans = 0; while(!Q.empty()){ int cur = Q.front(); Q.pop(); in[cur]--; int nex = -1; for(const auto &it: G[cur]){ if(in[it] > 0){ nex = it; break; } } if(nex == -1) break; if(c[cur] == 0){ c[cur] = c[cur]^1; c[nex] = c[nex]^1; ans++; } in[nex]--; if(in[nex] == 1) Q.emplace(nex); } cout << ans << endl; return 0; }