#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; template static void amin(T &x, U y) { if(y < x) x = y; } template static void amax(T &x, U y) { if(x < y) x = y; } vector t_parent; vi t_ord; void tree_getorder(const vector &g, int root) { int n = g.size(); t_parent.assign(n, -1); t_ord.clear(); vector stk; stk.push_back(root); while(!stk.empty()) { int i = stk.back(); stk.pop_back(); t_ord.push_back(i); for(int j = (int)g[i].size() - 1; j >= 0; j --) { int c = g[i][j]; if(t_parent[c] == -1 && c != root) stk.push_back(c); else t_parent[i] = c; } } } template void freeTreeDP(const vector &t_ord, const vector &t_parent, vector &downsum, vector &upsum, Sum identity, Combine combine, Calc calc) { int n = (int)t_ord.size(); vector children(n, 0), childid(n, -1); for(int ix = 1; ix < (int)t_ord.size(); ++ ix) { int i = t_ord[ix], p = t_parent[i]; childid[i] = children[p] ++; } vector> prefix(n), suffix(n); for(int i = 0; i < n; ++ i) { prefix[i].assign(children[i] + 1, identity); suffix[i].assign(children[i] + 1, identity); } downsum.assign(n, identity); upsum.assign(n, identity); for(int ix = (int)t_ord.size() - 1; ix >= 0; -- ix) { int i = t_ord[ix], p = t_parent[i]; for(int j = 0; j < children[i]; ++ j) prefix[i][j + 1] = combine(prefix[i][j], prefix[i][j + 1]); for(int j = children[i]; j > 0; -- j) suffix[i][j - 1] = combine(suffix[i][j - 1], suffix[i][j]); if(p != -1) { downsum[i] = calc(p, i, suffix[i][0]); prefix[p][childid[i] + 1] = downsum[i]; suffix[p][childid[i]] = downsum[i]; } } downsum[t_ord[0]] = suffix[t_ord[0]][0]; for(int ix = 1; ix < (int)t_ord.size(); ++ ix) { int i = t_ord[ix], p = t_parent[i]; Sum sum = suffix[p][childid[i] + 1]; sum = combine(sum, upsum[p]); sum = combine(sum, prefix[p][childid[i]]); upsum[i] = calc(i, p, sum); } } int main() { int N; while(~scanf("%d", &N)) { char S[100001]; scanf("%s", S); vector > g(N); for(int i = 0; i < N - 1; ++ i) { int u, v; scanf("%d%d", &u, &v), -- u, -- v; g[u].push_back(v); g[v].push_back(u); } tree_getorder(g, 0); vector downC, upC, downW, upW; freeTreeDP(t_ord, t_parent, downC, upC, 0, plus(), [&S](int from, int to, int sum) { return sum + (S[to] == 'c'); }); freeTreeDP(t_ord, t_parent, downW, upW, 0, plus(), [&S](int from, int to, int sum) { return sum + (S[to] == 'w'); }); ll ans = 0; rep(i, N) if(S[i] == 'w') { int sumC = 0, sumW = 0; ll sumProd = 0; for(int j : g[i]) { int c = j != t_parent[i] ? downC[j] : upC[i]; int w = j != t_parent[i] ? downW[j] : upW[i]; sumC += c; sumW += w; sumProd += (ll)c * w; } ans += (ll)sumC * sumW - sumProd; } printf("%lld\n", ans); } return 0; }