AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第9回PAST G 連結


問題へのリンク


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

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

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

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

        var sb = new System.Text.StringBuilder();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            int Type = wkArr[0];
            int FromNode = wkArr[1];
            int ToNode = wkArr[2];

            if (Type == 1) {
                bool IsConn = false;
                if (mToNodeListDict.ContainsKey(FromNode) == false) {
                    IsConn = true;
                }
                else if (mToNodeListDict[FromNode].Contains(ToNode) == false) {
                    IsConn = true;
                }

                // Connの場合
                if (IsConn) {
                    if (mToNodeListDict.ContainsKey(FromNode) == false) {
                        mToNodeListDict[FromNode] = new HashSet<int>();
                    }
                    if (mToNodeListDict.ContainsKey(ToNode) == false) {
                        mToNodeListDict[ToNode] = new HashSet<int>();
                    }
                    mToNodeListDict[FromNode].Add(ToNode);
                    mToNodeListDict[ToNode].Add(FromNode);
                }
                // DisConnの場合
                else {
                    mToNodeListDict[FromNode].Remove(ToNode);
                    mToNodeListDict[ToNode].Remove(FromNode);
                }
            }
            if (Type == 2) {
                if (CanMove(FromNode, ToNode)) {
                    sb.AppendLine("Yes");
                }
                else {
                    sb.AppendLine("No");
                }
            }
        }
        Console.Write(sb.ToString());
    }

    struct JyoutaiDef
    {
        internal int CurrNode;
    }

    static bool CanMove(int pStaNode, int pGoalNode)
    {
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrNode = pStaNode;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<int>();

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

            // クリア判定
            if (Popped.CurrNode == pGoalNode) return true;

            if (mToNodeListDict.ContainsKey(Popped.CurrNode)) {
                foreach (int EachToNode in mToNodeListDict[Popped.CurrNode]) {
                    if (VisitedSet.Add(EachToNode)) {
                        WillPush.CurrNode = EachToNode;
                        Stk.Push(WillPush);
                    }
                }
            }
        }
        return false;
    }
}


解説

隣接グラフで枝を管理して、
ConnectとDisConnctの対応を行ってます。

移動の可否判定は、毎回DFSを行ってます。