#include using namespace std; #define ll long long #define pii pair #define pll pair #define vi vector #define vl vector #define ov4(a, b, c, d, name, ...) name #define rep3(i, a, b, c) for(ll i = (a); i < (b); i += (c)) #define rep2(i, a, b) rep3(i, a, b, 1) #define rep1(i, n) rep2(i, 0, n) #define rep0(n) rep1(aaaaa, n) #define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__) #define per(i, a, b) for(ll i = (a) - 1; i >= (b); i--) #define fore(e, v) for(auto &&e : v) #define all(a) begin(a), end(a) #define si(a) (int)(size(a)) #define lb(v, x) (lower_bound(all(v), x) - begin(v)) #define eb emplace_back template bool chmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template bool chmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; } const int INF = 1e9 + 100; const ll INFL = 3e18 + 100; #define i128 __int128_t struct _ { _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); } } __; struct BIT { vi a; BIT(int n) : a(n + 1) {} void add(int i, int x) { i++; while(i < si(a)) a[i] += x, i += i & -i; } int sum(int r) { int s = 0; while(r) s += a[r], r -= r & -r; return s; } int sum(int l, int r) { return sum(r) - sum(l); } }; int main() { int n; cin >> n; vector g(n); rep(n - 1) { int x, y; cin >> x >> y; --x, --y; g[x].eb(y), g[y].eb(x); } string s; cin >> s; vi a(n); rep(i, n) a[i] = (s[i] == '1' ? 1 : -1); if(n == 1) { cout << count(all(a), 1) << endl; exit(0); } vi sub(n), v(n); auto dfs = [&](auto &&f, int x, int p) -> void { v[x] += a[x]; sub[x]++; if(g[x][0] == p) swap(g[x][0], g[x].back()); fore(e, g[x]) { if(e == p) continue; v[e] = v[x]; f(f, e, x); sub[x] += sub[e]; if(sub[e] > sub[g[x][0]]) swap(e, g[x][0]); } return; }; dfs(dfs, 0, -1); vi in(n), out(n), tour(n); int idx = 0; { auto dfs = [&](auto &&f, int x, int p) -> void { tour[idx] = x; in[x] = idx++; out[x] = in[x] + sub[x]; fore(e, g[x]) if(e != p) f(f, e, x); }; dfs(dfs, 0, -1); } ll ans = count(all(a), 1); BIT bit(n * 3 + 1); int car = n * 1.5; vi tmp; // rep(i, n) cout << v[i] << " "; // cout << endl; auto add = [&](int i) { bit.add(i + car, 1); tmp.eb(i + car); }; auto dfs2 = [&](auto &&f, int x, int p, bool clear) -> void { rep(i, 1, si(g[x])) { int e = g[x][i]; if(e == p) continue; f(f, e, x, true); } if(g[x][0] != p) f(f, g[x][0], x, false); ll tans = 0; tans += bit.sum(v[x] - a[x] + car + 1, 4 * n + 1); add(v[x]); rep(i, 1, si(g[x])) { int e = g[x][i]; if(e == p) continue; rep(j, in[e], out[e]) { int t = tour[j]; int tar = 2 * v[x] - a[x] - v[t]; tans += bit.sum(tar + car + 1, 4 * n + 1); } rep(j, in[e], out[e]) add(v[tour[j]]); } // cout << x << ": " << tans << endl; ans += tans; if(clear) { fore(i, tmp) bit.add(i, -1); tmp.clear(); } }; dfs2(dfs2, 0, -1, true); cout << ans << endl; }