def rerooting(g,e_M): """ 以下を書き換える """ def merge(x,y): #値 x に y をマージ return (x[0]+y[0], x[1]+y[1], x[2]+y[2]+x[0]*y[1]+x[1]*y[0]) def h1(x,v): # x = dp[v][p] を親方向にマージする際の補正 return (x[0]+1,x[1],0) if s[v]=="c" else (x[0],x[1]+1,0) def h2(x,v): # x = dp[p][v] を子方向にマージする際の補正 return (x[0]+1,x[1],0) if s[parent[v]]=="c" else (x[0],x[1]+1,0) """ dfs_bottom_up: 木DP dfs_top_down: rerooting """ def get_order(g): n = len(g) order = [] st = [0] parent = [-1]*n while st: v = st.pop() order.append(v) for c in g[v]: if c != parent[v]: st.append(c) parent[c] = v return order, parent def dfs_bottom_up(BU,hBU,parent,order): for i in order[::-1]: hBU[i] = h1(BU[i], i) if i: BU[parent[i]] = merge(BU[parent[i]], hBU[i]) def dfs_top_down(hBU,hTD,g,parent,order): for v in order: acc_l = e_M for c in g[v]: if c==parent[v]: acc_l = merge(acc_l,hTD[v]) else: hTD[c] = acc_l acc_l = merge(acc_l,hBU[c]) acc_r = e_M for c in g[v][::-1]: if c==parent[v]: acc_r = merge(hTD[v],acc_r) else: hTD[c] = h2(merge(hTD[c],acc_r),c) acc_r = merge(acc_r,hBU[c]) n = len(g) order, parent = get_order(g) BU = [e_M]*n hBU = [e_M]*n hTD = [e_M]*n dfs_bottom_up(BU,hBU,parent,order) dfs_top_down(hBU,hTD,g,parent,order) for i in range(n): BU[i] = merge(BU[i],hTD[i]) return BU n = int(input()) s = input() g = [[] for _ in range(n)] for _ in range(n-1): a,b = map(int,input().split()) g[a-1].append(b-1) g[b-1].append(a-1) BU = rerooting(g,(0,0,0)) ans = 0 for i in range(n): if s[i] == "w": ans += BU[i][2] print(ans)