AtCoderのABC
   次のABCの問題へ
   前のABCの問題へ
ABC198-E Unique Color
C#のソース
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static string InputPattern = "InputX";
    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();
        if (InputPattern == "Input1") {
            WillReturn.Add("6");
            WillReturn.Add("2 7 1 8 2 8");
            WillReturn.Add("1 2");
            WillReturn.Add("3 6");
            WillReturn.Add("3 2");
            WillReturn.Add("4 3");
            WillReturn.Add("2 5");
            //1
            //2
            //3
            //4
            //6
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10");
            WillReturn.Add("3 1 4 1 5 9 2 6 5 3");
            WillReturn.Add("1 2");
            WillReturn.Add("2 3");
            WillReturn.Add("3 4");
            WillReturn.Add("4 5");
            WillReturn.Add("5 6");
            WillReturn.Add("6 7");
            WillReturn.Add("7 8");
            WillReturn.Add("8 9");
            WillReturn.Add("9 10");
            //1
            //2
            //3
            //5
            //6
            //7
            //8
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }
    // 隣接リスト
    static Dictionary<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();
    static int[] mColorArr;
    static void Main()
    {
        List<string> InputList = GetInputList();
        mColorArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
        foreach (string EachStr in InputList.Skip(2)) {
            SplitAct(EachStr);
            int FromNode = wkArr[0];
            int ToNode = wkArr[1];
            if (mToNodeListDict.ContainsKey(FromNode) == false) {
                mToNodeListDict[FromNode] = new List<int>();
            }
            if (mToNodeListDict.ContainsKey(ToNode) == false) {
                mToNodeListDict[ToNode] = new List<int>();
            }
            mToNodeListDict[FromNode].Add(ToNode);
            mToNodeListDict[ToNode].Add(FromNode);
        }
        DFS(1);
        mAnswerNodeList.Sort();
        mAnswerNodeList.ForEach(pX => Console.WriteLine(pX));
    }
    // 件数[色]なDict
    static Dictionary<int, int> mColorCntDict = new Dictionary<int, int>();
    // 訪問済ノード
    static HashSet<int> mVisitedNodeSet = new HashSet<int>();
    static List<int> mAnswerNodeList = new List<int>();
    static void DFS(int pCurrNode)
    {
        // ルートノードの分があるので、ここでもAddの必要あり
        mVisitedNodeSet.Add(pCurrNode);
        int CurrColor = mColorArr[pCurrNode - 1];
        if (mColorCntDict.ContainsKey(CurrColor) == false) {
            mColorCntDict[CurrColor] = 0;
        }
        mColorCntDict[CurrColor]++;
        if (mColorCntDict[CurrColor] == 1) {
            mAnswerNodeList.Add(pCurrNode);
        }
        // 接続ノードへの訪問
        foreach (int EachToNode in mToNodeListDict[pCurrNode]) {
            if (mVisitedNodeSet.Add(EachToNode) == false) {
                continue;
            }
            DFS(EachToNode);
        }
        // 帰りがけの処理
        mColorCntDict[CurrColor]--;
    }
}
解説
帰りがけの処理があるので、再帰関数で深さ優先探索してます。