典型90問    次の典型90問へ    前の典型90問へ

典型90問 026 Independent Set on a Tree(★4)


問題へのリンク


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("4");
            WillReturn.Add("1 2");
            WillReturn.Add("2 3");
            WillReturn.Add("2 4");
            //3 4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("6");
            WillReturn.Add("1 3");
            WillReturn.Add("2 4");
            WillReturn.Add("3 5");
            WillReturn.Add("2 5");
            WillReturn.Add("3 6");
            //1 2 6
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int mN;

    // 隣接リスト
    static Dictionary<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();

    static void Main()
    {
        List<string> InputList = GetInputList();

        mN = int.Parse(InputList[0]);

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        foreach (string EachStr in InputList.Skip(1)) {
            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);
        }

        ExecDFS(1);

        var Mod0List = new List<int>();
        var Mod1List = new List<int>();

        foreach (var EachPair in mLevelMod2Dict) {
            if (EachPair.Value == 0) {
                Mod0List.Add(EachPair.Key);
            }
            if (EachPair.Value == 1) {
                Mod1List.Add(EachPair.Key);
            }
        }

        List<int> PList = Mod0List;
        if (Mod0List.Count < Mod1List.Count) {
            PList = Mod1List;
        }

        var AnswerList = new List<int>();
        for (int I = 1; I <= mN / 2; I++) {
            AnswerList.Add(PList[I - 1]);
        }
        Console.WriteLine(IntEnumJoin(" ", AnswerList));
    }

    // セパレータとInt型の列挙を引数として、結合したstringを返す
    static string IntEnumJoin(string pSeparater, IEnumerable<int> pEnum)
    {
        string[] StrArr = Array.ConvertAll(pEnum.ToArray(), pX => pX.ToString());
        return string.Join(pSeparater, StrArr);
    }

    // レベル[ノード]なDict
    static Dictionary<int, int> mLevelMod2Dict = new Dictionary<int, int>();

    struct JyoutaiDef
    {
        internal int CurrNode;
        internal int Level;
    }

    static void ExecDFS(int pStaNode)
    {
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrNode = pStaNode;
        WillPush.Level = 1;
        Stk.Push(WillPush);

        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();

            // 訪問済の場合
            if (mLevelMod2Dict.ContainsKey(Popped.CurrNode)) {
                continue;
            }

            // 未訪問なら採色
            mLevelMod2Dict[Popped.CurrNode] = Popped.Level % 2;

            if (mToNodeListDict.ContainsKey(Popped.CurrNode) == false) {
                continue;
            }
            foreach (int EachToNode in mToNodeListDict[Popped.CurrNode]) {
                WillPush.CurrNode = EachToNode;
                WillPush.Level = Popped.Level + 1;
                Stk.Push(WillPush);
            }
        }
    }
}


解説

木のノードは、2色で塗り分けできるので
多い色のノードを列挙すれば良いです。