#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define ll long long int #define ti4 tuple #define pii pair #define REP(x,n) for(int x = 0;x < n;x++) struct UnionFind { vector data; UnionFind(ll size) : data(size, -1) { } bool unionSet(ll x, ll y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(ll x, ll y) { return root(x) == root(y); } ll root(ll x) { return data[x] < 0 ? x : data[x] = root(data[x]); } ll size(ll x) { return -data[root(x)]; } }; #define MAX_COUNT 1000001 string ss; vector g[MAX_COUNT]; int cc[MAX_COUNT]; int cw[MAX_COUNT]; ll ret; ll tc; ll tw; void dfs(int current, int prev) { if(ss[current] == 'w') cw[current]=1; else if(ss[current] == 'c') cc[current]=1; for(auto e : g[current]) { if(e == prev) { continue; } dfs(e, current); cw[current] += cw[e]; cc[current] += cc[e]; if(ss[current] == 'w') { ret += cc[e] * (tw-1-cw[e]); } } if(prev >= 0 && ss[current] == 'w') { ret += (tc-cc[current]) * (cw[current]-1); } } int main() { ret = 0; tc = tw = 0; fill(cc, cc+MAX_COUNT,0); fill(cw, cw+MAX_COUNT,0); int n; cin >> n; cin >> ss; for(auto r : ss) { if(r == 'w') { tw++; } else { tc++; } } for(int i = 0;i < n-1;i++) { int a,b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } dfs(0,-1); cout << ret << endl; return 0; }