AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC190-C Bowls and Dishes


問題へのリンク


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

    struct ABInfoDef
    {
        internal int A;
        internal int B;
    }

    struct CDInfoDef
    {
        internal int C;
        internal int D;
    }

    static List<ABInfoDef> mABInfoList = new List<ABInfoDef>();
    static List<CDInfoDef> mCDInfoList = new List<CDInfoDef>();

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

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

        SplitAct(InputList[0]);
        int M = wkArr[1];

        foreach (string EachStr in InputList.Skip(1).Take(M)) {
            SplitAct(EachStr);
            ABInfoDef WillAdd;
            WillAdd.A = wkArr[0];
            WillAdd.B = wkArr[1];
            mABInfoList.Add(WillAdd);
        }

        foreach (string EachStr in InputList.Skip(1 + M + 1)) {
            SplitAct(EachStr);
            CDInfoDef WillAdd;
            WillAdd.C = wkArr[0];
            WillAdd.D = wkArr[1];
            mCDInfoList.Add(WillAdd);
        }

        int Answer = int.MinValue;
        List<HashSet<int>> SaraSetList = ExecDFS();
        foreach (HashSet<int> EachSaraSet in SaraSetList) {
            int CurrCnt = mABInfoList.Count(
                pX => EachSaraSet.Contains(pX.A) &&
                      EachSaraSet.Contains(pX.B));

            Answer = Math.Max(Answer, CurrCnt);
        }
        Console.WriteLine(Answer);
    }

    struct JyoutaiDef
    {
        internal int CurrInd;
        internal HashSet<int> SaraSet;
    }

    // DFSで置いた皿SetのListを返す
    static List<HashSet<int>> ExecDFS()
    {
        var WillReturn = new List<HashSet<int>>();

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrInd = 0;
        WillPush.SaraSet = new HashSet<int>();
        Stk.Push(WillPush);

        int UB = mCDInfoList.Count - 1;
        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();

            if (Popped.CurrInd > UB) {
                WillReturn.Add(Popped.SaraSet);
                continue;
            }
            int SaraC = mCDInfoList[Popped.CurrInd].C;
            int SaraD = mCDInfoList[Popped.CurrInd].D;

            WillPush.CurrInd = Popped.CurrInd + 1;
            WillPush.SaraSet = new HashSet<int>(Popped.SaraSet) { SaraC };
            Stk.Push(WillPush);

            WillPush.CurrInd = Popped.CurrInd + 1;
            WillPush.SaraSet = new HashSet<int>(Popped.SaraSet) { SaraD };
            Stk.Push(WillPush);
        }
        return WillReturn;
    }
}


解説

深さ優先探索で、全組合せを列挙してます。