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

ABC395-D Pigeon Swap


問題へのリンク


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("6 8");
            WillReturn.Add("1 2 4");
            WillReturn.Add("1 3 6");
            WillReturn.Add("3 2");
            WillReturn.Add("2 4 5");
            WillReturn.Add("3 2");
            WillReturn.Add("1 4 2");
            WillReturn.Add("3 4");
            WillReturn.Add("3 2");
            //4
            //5
            //2
            //5
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("1 2");
            WillReturn.Add("1 1 1");
            WillReturn.Add("3 1");
            //1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("30 15");
            WillReturn.Add("3 3");
            WillReturn.Add("2 8 30");
            WillReturn.Add("2 12 15");
            WillReturn.Add("2 2 17");
            WillReturn.Add("1 19 1");
            WillReturn.Add("2 7 30");
            WillReturn.Add("3 12");
            WillReturn.Add("3 8");
            WillReturn.Add("2 25 26");
            WillReturn.Add("1 13 10");
            WillReturn.Add("1 16 10");
            WillReturn.Add("2 16 29");
            WillReturn.Add("2 1 21");
            WillReturn.Add("2 6 11");
            WillReturn.Add("1 21 8");
            //3
            //15
            //7
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    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 N = wkArr[0];

        // 左から何番目の巣にいるか[ハト]
        var Dict1 = new Dictionary<int, int>();

        // 巣の名前[左からの番目]
        var Dict2 = new Dictionary<int, int>();

        // 左からの番目[巣の名前]
        var Dict3 = new Dictionary<int, int>();

        for (int I = 1; I <= N; I++) {
            Dict1[I] = I;
            Dict2[I] = I;
            Dict3[I] = I;
        }

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            int Type = wkArr[0];
            if (Type == 1) {
                int A = wkArr[1];
                int B = wkArr[2];
                Dict1[A] = Dict3[B];
            }
            if (Type == 2) {
                int A = wkArr[1];
                int B = wkArr[2];

                int APos = Dict3[A];
                int BPos = Dict3[B];

                Dict3[A] = BPos;
                Dict3[B] = APos;

                Dict2[APos] = B;
                Dict2[BPos] = A;
            }
            if (Type == 3) {
                int A = wkArr[1];
                int Pos = Dict1[A];
                Console.WriteLine(Dict2[Pos]);
            }
        }
    }
}


解説

下記の3つのDictを用意してます。

●左から何番目の巣にいるか[ハト]
●巣の名前[左からの番目]
●左からの番目[巣の名前]