AtCoderのABC    次のABCの問題へ

ABC002-D 派閥


問題へのリンク


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

    static int mN;

    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]);
        mN = wkArr[0];
        int UB = mN;

        bool[,] MatrixArr = new bool[UB + 1, UB + 1];
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            int X = wkArr[0];
            int Y = wkArr[1];
            MatrixArr[X, Y] = true;
        }

        int Answer = 0;
        List<int[]> KouhoArrList = ExecBitZentansaku();
        foreach (int[] EachKouhoArr in KouhoArrList) {
            bool IsOK = true;
            for (int I = 0; I <= EachKouhoArr.GetUpperBound(0); I++) {
                for (int J = I + 1; J <= EachKouhoArr.GetUpperBound(0); J++) {
                    int X = EachKouhoArr[I];
                    int Y = EachKouhoArr[J];
                    if (MatrixArr[X, Y] == false) {
                        IsOK = false;
                    }
                }
            }
            if (IsOK) {
                Answer = Math.Max(Answer, EachKouhoArr.Length);
            }
        }
        Console.WriteLine(Answer);
    }

    // ビット全探索
    static List<int[]> ExecBitZentansaku()
    {
        var WillReturn = new List<int[]>();

        int MaxVal = (1 << mN) - 1;

        for (int I = 0; I <= MaxVal; I++) {
            var CurrList = new List<int>();
            for (int J = 1; J <= mN; J++) {
                int CurrBit = (1 << (J - 1));

                if ((I & CurrBit) > 0) {
                    CurrList.Add(J);
                }
            }
            WillReturn.Add(CurrList.ToArray());
        }
        return WillReturn;
    }
}


解説

隣接行列で辺を持っておき、
ビット全探索で全ての候補を調べてます。