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

ABC282-D Make Bipartite 2


問題へのリンク


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

    static long mN;

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

    static List<string> mInputList;

    static void Main()
    {
        mInputList = GetInputList();

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

        SplitAct(mInputList[0]);
        mN = wkArr[0];

        foreach (string EachStr in mInputList.Skip(1)) {
            SplitAct(EachStr);
            long FromNode = wkArr[0];
            long ToNode = wkArr[1];

            if (mToNodeListDict.ContainsKey(FromNode) == false) {
                mToNodeListDict[FromNode] = new List<long>();
            }
            if (mToNodeListDict.ContainsKey(ToNode) == false) {
                mToNodeListDict[ToNode] = new List<long>();
            }
            mToNodeListDict[FromNode].Add(ToNode);
            mToNodeListDict[ToNode].Add(FromNode);
        }

        // 2部グラフかの判定を行う
        for (long I = 1; I <= mN; I++) {
            if (mLevelMod2Dict.ContainsKey(I)) {
                continue;
            }
            bool Result = ExecDFS(I);
            if (Result == false) {
                Console.WriteLine(0);
                return;
            }
        }

        Solve();
    }

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

    // 根ノード[ノード]なDict
    static Dictionary<long, long> mRootNodeDict = new Dictionary<long, long>();

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

    static bool ExecDFS(long 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)) {
                long CorrectLevelMod2 = mLevelMod2Dict[Popped.CurrNode];
                if (CorrectLevelMod2 != Popped.Level % 2) {
                    return false;
                }
                continue;
            }

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

            mRootNodeDict[Popped.CurrNode] = pStaNode;

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

    // 連結成分ごとの情報
    class RenketuseibunInfo
    {
        internal long BlackNodeCnt;
        internal long WhiteNodeCnt;
        internal long EdgeCnt;
    }

    static void Solve()
    {
        var RenketuseibunInfoDict = new Dictionary<long, RenketuseibunInfo>();

        for (long I = 1; I <= mN; I++) {
            long RootNode = mRootNodeDict[I];
            if (RenketuseibunInfoDict.ContainsKey(RootNode) == false) {
                RenketuseibunInfoDict[RootNode] = new RenketuseibunInfo();
            }

            // 白ノードの場合
            if (mLevelMod2Dict[I] == 0) {
                RenketuseibunInfoDict[RootNode].WhiteNodeCnt++;
            }
            // 黒ノードの場合
            if (mLevelMod2Dict[I] == 1) {
                RenketuseibunInfoDict[RootNode].BlackNodeCnt++;
            }
        }

        // 連結成分ごとの枝の本数を数える
        foreach (string EachStr in mInputList.Skip(1)) {
            string[] SplitArr = EachStr.Split(' ');
            long FromNode = long.Parse(SplitArr[0]);

            long RootNode = mRootNodeDict[FromNode];
            RenketuseibunInfoDict[RootNode].EdgeCnt++;
        }

        long Answer1 = 0;
        long Answer2 = 0;

        foreach (RenketuseibunInfo EachRenketuseibunInfo in RenketuseibunInfoDict.Values) {
            long NodeCnt = EachRenketuseibunInfo.WhiteNodeCnt + EachRenketuseibunInfo.BlackNodeCnt;
            Answer1 += (mN - NodeCnt) * NodeCnt;

            long PairCnt = EachRenketuseibunInfo.BlackNodeCnt * EachRenketuseibunInfo.WhiteNodeCnt;
            Answer2 += PairCnt - EachRenketuseibunInfo.EdgeCnt;
        }
        Console.WriteLine(Answer1 / 2 + Answer2);
    }
}


解説

最初に、連結成分ごとに2部グラフかを判定します。

次に、連結成分ごとの、白ノード数と黒ノード数を求めます。
また、連結成分ごとの枝の数も求めます。

最後に、
違う連結成分を結ぶ辺の数と
同じ連結成分を結ぶ辺の数を
集計すれば解が求まります。