using System; using System.Linq; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Text; public class Program { public void Proc() { Reader.IsDebug = false; int nodeCount = int.Parse(Reader.ReadLine()); this.TreeList = new TreeNode[nodeCount+1]; string cwwText = Reader.ReadLine(); for(int i=0; iint.Parse(a)).OrderBy(a=>a).ToArray(); int idx1 = inpt[0]; int idx2 = inpt[1]; if(TreeList[idx2].Parent == null) { TreeList[idx1].Add(TreeList[idx2]); } else if(TreeList[idx1].Parent == null) { TreeList[idx2].Add(TreeList[idx1]); } else { TreeList[idx2].UpsideDown(); TreeList[idx1].Add(TreeList[idx2]); } } long ans = 0; foreach(TreeNode cnode in this.CList) { cnode.UpsideDown(); ans+=GetAns(cnode, 2); } Console.WriteLine(ans); } private long GetAns(TreeNode nd, int remainW) { long ans = 0; if(nd.Type == TreeNodeType.W && remainW == 1) { ans+=1; } if(nd.Type == TreeNodeType.W && remainW == 2) { nd.Items.ForEach(a=>ans+=GetAns(a, remainW-1)); } nd.Items.ForEach(a=>ans+=GetAns(a, remainW)); return ans; } private TreeNode[] TreeList; private List CList = new List(); public class TreeNode { public TreeNode Parent; public int ID; public TreeNodeType Type; public List Items = new List(); public TreeNode UpsideDown() { if(this.Parent == null) { return null; } TreeNode target = this.Parent; target.Items.Remove(this); this.Parent = null; target.UpsideDown(); this.Add(target); return target; } public void Add(TreeNode target) { target.Parent = this; this.Items.Add(target); } public TreeNode(int id, char c) { this.ID = id; this.Type = c=='c'?TreeNodeType.C:TreeNodeType.W; } } public enum TreeNodeType { C, W, } public class Reader { public static bool IsDebug = true; private static System.IO.StringReader SReader; private static string InitText = @" 3 wcw 1 2 3 2 "; public static string ReadLine() { if(IsDebug) { if(SReader == null) { SReader = new System.IO.StringReader(InitText.Trim()); } return SReader.ReadLine(); } else { return Console.ReadLine(); } } } public static void Main(string[] args) { Program prg = new Program(); prg.Proc(); } }