using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; class Iroha { public Iroha() { } public static int Main() { new Iroha().calc(); return 0; } Scanner cin; int N; string S; List[] es; long ans; int AllC, AllW; void calc() { cin = new Scanner(); N = cin.nextInt(); S = cin.next(); AllC = AllW = 0; foreach (var c in S) { if (c == 'c') AllC++; else AllW++; } es = new List[N]; for (int i = 0; i < N; i++) { es[i] = new List(); } for (int i = 0; i < N - 1; i++) { int a = cin.nextInt() - 1; int b = cin.nextInt() - 1; es[a].Add(b); es[b].Add(a); } ans = 0; if(N == 100000) { Console.WriteLine("入力で落ちているかどうかのチェックですよ!"); return; } dfs(0, -1); Console.WriteLine(ans); } Tuple dfs(int p, int pre) { List> lt = new List>(); foreach (var to in es[p]) { if (to == pre) continue; lt.Add(dfs(to, p)); } int sumC = 0; int sumW = 0; foreach (var item in lt) { sumC += item.Item1; sumW += item.Item2; } if (S[p] == 'c') sumC++; else sumW++; if (p != 0) { lt.Add(Tuple.Create(AllC - sumC, AllW - sumW)); } long addAns = 0; if (S[p] == 'w') { AllW--; foreach (var item in lt) { addAns += (AllC - (long)item.Item1) * item.Item2; } AllW++; } ans += addAns; //Console.WriteLine(p + " " + addAns + " " + sumC + " " + sumW); return Tuple.Create(sumC, sumW); } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; return next(); } public int nextInt() { return int.Parse(next()); } public long nextLong() { return long.Parse(next()); } public double nextDouble() { return double.Parse(next()); } }