#include #include "atcoder/all" #define debug cout << "OK" << endl; template inline bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; } inline int Code(char c) { if ('A' <= c and c <= 'Z')return (int)(c - 'A'); if ('a' <= c and c <= 'z')return (int)(c - 'a'); if ('0' <= c and c <= '9')return (int)(c - '0'); assert(false); } using namespace std; using namespace atcoder; #define int long long constexpr int MOD = 998244353; constexpr int INF = (1LL << 62); using minit = modint998244353; template ostream& operator << (ostream& st, const vector &v) { for (const T value : v) { st << value << " "; } return st; } template istream& operator >> (istream& st, vector& v) { for (T &value : v) { st >> value; } return st; } // // void solve() { //#1 入力 int N; cin >> N; vector> G(N); for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } vector color(N); cin >> color; //#2 再帰 function(int, int)> dfs = [&](int now, int par) { //nowを根とする部分木を黒くするのに必要な操作回数 // first...根は黒にできるか // second...最小の操作回数 int res = 0, cnt = 0; for (const int next : G[now]) { if (next == par)continue; pair p = dfs(next, now); cnt += 1 - p.first; res += p.second; } return make_pair((cnt ^ color[now]) & 1, res + cnt); }; //#3 出力 pair p = dfs(0, -1); if (p.first)cout << p.second << endl; else cout << -1 << endl; } signed main() { int T = 1; // cin >> T; for (int i = 0; i < T; i++) solve(); return 0; }