#include using namespace std; int main() { int N; cin >> N; string S; cin >> S; int all_w = 0; for (int i = 0; i < N; i++) { if (S[i] == 'w') { all_w++; } } vector> E(N); for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; u--; v--; E[u].push_back(v); E[v].push_back(u); } queue Q; int zero = 0; for (int i = 0; i < N; i++) { if (S[i] == 'c') { zero = i; break; } } Q.push(zero); vector p(N, -1); vector> c(N); vector bfs; while (!Q.empty()) { int v = Q.front(); Q.pop(); bfs.push_back(v); for (int w : E[v]) { if (w != p[v]) { p[w] = v; c[v].push_back(w); Q.push(w); } } } reverse(bfs.begin(), bfs.end()); vector dp1(N);//w vector dp2(N);//ww for (int v : bfs) { for (int w : c[v]) { dp1[v] += dp1[w]; if (S[v] == 'w') { dp2[v] += dp1[w]; } dp2[v] += dp2[w]; } if (S[v] == 'w') { dp1[v]++; } } reverse(bfs.begin(), bfs.end()); vector dp3(N);//辺を張り替えてからのww for (int v : bfs) { //S[v] == 'w'かどうかで変わる。 long long sum = 0; long long cntw = 0; for (int w : c[v]) { sum += dp2[w]; cntw += dp1[w]; } for (int w : c[v]) { long long sum1 = cntw - dp1[w]; long long sum2 = sum - dp2[w]; if (S[v] == 'w') { sum2 += sum1; sum2 += all_w - dp1[v]; } sum2 += dp3[v]; dp3[w] = sum2; } } long long ans = 0; for (int i = 0; i < N; i++) { if (S[i] == 'c') { ans += dp2[i] + dp3[i]; } } cout << ans << endl; }