/* -*- coding: utf-8 -*- * * 439.cc: No.439 チワワのなる木 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; /* typedef */ typedef long long ll; typedef vector vi; typedef pair pii; /* global variables */ int n, tcn, twn; string s; vi nbrs[MAX_N]; int cis[MAX_N], wis[MAX_N], ccs[MAX_N], wcs[MAX_N]; /* subroutines */ void rec(int u, int p) { vi &nbru = nbrs[u]; for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int &v = *vit; if (v == p) continue; rec(v, u); ccs[u] += cis[v] + ccs[v]; wcs[u] += wis[v] + wcs[v]; } } /* main */ int main() { cin >> n >> s; for (int i = 0; i < n; i++) (s[i] == 'c') ? (cis[i] = 1, tcn++) : (wis[i] = 1, twn++); //printf("tcn=%d, twn=%d\n", tcn, twn); for (int i = 0; i < n - 1; i++) { int ai, bi; cin >> ai >> bi; ai--, bi--; nbrs[ai].push_back(bi); nbrs[bi].push_back(ai); } rec(0, -1); //for (int i = 0; i < n; i++) //printf("ccs[%d]=%d, wcs[%d]=%d\n", i, ccs[i], i, wcs[i]); queue q; q.push(pii(0, -1)); ll ans = 0; while (! q.empty()) { pii u = q.front(); q.pop(); int &ui = u.first, &up = u.second; int con = tcn - cis[ui], won = twn - wis[ui]; if (wis[ui]) ans += (ll)(con - ccs[ui]) * wcs[ui]; vi &nbru = nbrs[ui]; for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int &vi = *vit; if (vi == up) continue; if (wis[ui]) ans += (ll)ccs[vi] * (won - wcs[vi]); q.push(pii(vi, ui)); } } printf("%lld\n", ans); return 0; }