import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.container; // SList, DList, BinaryHeap void main() { auto n = readln.chomp.to!int; auto s = readln.chomp; auto children = new int[][](n); foreach (_; 0..n-1) { auto rd = readln.splitter; auto a = rd.front.to!int-1; rd.popFront(); auto b = rd.front.to!int-1; children[a] ~= b; children[b] ~= a; } auto parent = new int[](n); parent[0] = -1; auto ac = new int[](n), aw = new int[](n); auto q = DList!int(0), st = SList!int(); while (!q.empty) { auto u = q.front; q.removeFront(); st.insertFront(u); foreach (v; children[u]) if (v != parent[u]) { parent[v] = u; ac[v] = ac[u] + (s[u] == 'c'); aw[v] = aw[u] + (s[u] == 'w'); q.insertBack(v); } } auto dc = new int[](n), dw = new int[](n); while (!st.empty) { auto u = st.front; st.removeFront(); foreach (v; children[u]) if (v != parent[u]) { dc[u] += dc[v] + (s[v] == 'c'); dw[u] += dw[v] + (s[v] == 'w'); } } auto ans = 0L; foreach (u; 0..n) if (s[u] == 'w') { ans += long(ac[u]) * dw[u]; ans += long(aw[u]) * dc[u]; ans += long(dc[u]) * dw[u]; foreach (v; children[u]) if (v != parent[u]) ans -= long(dc[v] + (s[v] == 'c')) * (dw[v] + (s[v] == 'w')); } writeln(ans); }