典型問題    次の典型問題へ    前の典型問題へ

典型問題(上級) 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("6");
            WillReturn.Add("1 2");
            WillReturn.Add("1 3");
            WillReturn.Add("2 4");
            WillReturn.Add("3 5");
            WillReturn.Add("3 6");
            WillReturn.Add("3");
            WillReturn.Add("4 6");
            WillReturn.Add("5 6");
            WillReturn.Add("1 5");
            //1
            //3
            //1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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


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

        long NodeCnt = long.Parse(InputList[0]);

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

        foreach (string EachStr in InputList.Skip(1).Take((int)NodeCnt - 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);
        }

        // 第1引数は、根ノード
        // 第2引数は、親ノード (根ノードの場合は -1
        // 第3引数は、レベル   (根ノードの場合は 1)
        ExecEulerTour(1, -1, 1);

        // 最初に登場するInd[ノード]なDict
        var FirstApperIndDict = new Dictionary<long, long>();

        // Level[ノード]なDict
        var LevelDict = new Dictionary<long, long>();

        // スパーステーブルのコンストラクタに使うLevelのList
        var LevelList = new List<long>();
        for (int I = 0; I <= mEulerTourJyoutaiList.Count - 1; I++) {
            long CurrNode = mEulerTourJyoutaiList[I].CurrNode;
            if (FirstApperIndDict.ContainsKey(CurrNode) == false) {
                FirstApperIndDict[CurrNode] = I;
            }
            LevelDict[CurrNode] = mEulerTourJyoutaiList[I].Level;
            LevelList.Add(mEulerTourJyoutaiList[I].Level);
        }

        var InsSparseTable = new SparseTable<long>(LevelList, true);

        foreach (string EachStr in InputList.Skip(1 + (int)NodeCnt)) {
            SplitAct(EachStr);
            long Node1 = wkArr[0];
            long Node2 = wkArr[1];

            long Ind1 = FirstApperIndDict[Node1];
            long Ind2 = FirstApperIndDict[Node2];

            long LeftInd = Math.Min(Ind1, Ind2);
            long RightInd = Math.Max(Ind1, Ind2);

            long LCALevel = InsSparseTable.Query(LeftInd, RightInd);

            while (LeftInd + 1 < RightInd) {
                long Mid = (LeftInd + RightInd) / 2;
                long MinLeft = InsSparseTable.Query(LeftInd, Mid);

                if (MinLeft == LCALevel) {
                    RightInd = Mid;
                }
                else {
                    LeftInd = Mid;
                }
            }
            if (InsSparseTable.Query(LeftInd, LeftInd) == LCALevel) {
                Console.WriteLine(mEulerTourJyoutaiList[(int)LeftInd].CurrNode);
            }
            else {
                Console.WriteLine(mEulerTourJyoutaiList[(int)RightInd].CurrNode);
            }
        }
    }

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

    // DFSを行い、下記の2通りのタイミングでノードをAddする
    // 1 最初の訪問時
    // 2 子の部分木を訪問時
    static List<JyoutaiDef_EulerTour> mEulerTourJyoutaiList = new List<JyoutaiDef_EulerTour>();
    static void ExecEulerTour(long pCurrNode, long pParentNode, long pLevel)
    {
        JyoutaiDef_EulerTour WillAdd;
        WillAdd.CurrNode = pCurrNode;
        WillAdd.Level = pLevel;
        mEulerTourJyoutaiList.Add(WillAdd);
        if (mToNodeListDict.ContainsKey(pCurrNode)) {
            foreach (long EachToNode in mToNodeListDict[pCurrNode]) {
                if (EachToNode == pParentNode) continue;
                ExecEulerTour(EachToNode, pCurrNode, pLevel + 1);
                mEulerTourJyoutaiList.Add(WillAdd);
            }
        }
    }
}

#region SparseTable
internal class SparseTable<Type> where Type : IComparable<Type>
{
    // 最小値か最大値[2の指数][開始Ind]なArr
    private Type[][] mMinOrMaxJagArr;

    // Log2の値[2べき値] なDict
    private Dictionary<long, long> mLogDict = new Dictionary<long, long>();

    // 対応する2べき値[クエリの区間長]
    private long[] mBeki2Arr;

    // 最小値を取得するか?
    private bool mIsMin = false;

    // コンストラクタ1(配列が引数)
    internal SparseTable(Type[] pInitArr, bool pIsMin)
    {
        mIsMin = pIsMin;

        long Sisuu = 0;
        long Beki2 = 1;
        while (true) {
            mLogDict[Beki2] = Sisuu;
            Beki2 *= 2;

            if (Beki2 > pInitArr.Length) {
                break;
            }
            Sisuu++;
        }

        mMinOrMaxJagArr = new Type[Sisuu + 1][];
        foreach (var EachPair in mLogDict) {
            long CurrBeki2 = EachPair.Key;
            long CurrSisuu = EachPair.Value;
            long CurrUB = pInitArr.GetUpperBound(0) - CurrBeki2 + 1;
            mMinOrMaxJagArr[CurrSisuu] = new Type[CurrUB + 1];
        }

        // 長さ1の分を初期化
        for (long I = 0; I <= pInitArr.GetUpperBound(0); I++) {
            mMinOrMaxJagArr[0][I] = pInitArr[I];
        }

        // コンストラクタのヘルパメソッド
        ConstructorHelper();

        // 対応する2べき値[クエリの区間長]を設定
        SetBeki2Arr(pInitArr.Length);
    }

    // コンストラクタ2(Listが引数)
    internal SparseTable(List<Type> pInitList, bool pIsMin)
    {
        mIsMin = pIsMin;

        long Sisuu = 0;
        long Beki2 = 1;
        while (true) {
            mLogDict[Beki2] = Sisuu;
            Beki2 *= 2;

            if (Beki2 > pInitList.Count) {
                break;
            }
            Sisuu++;
        }

        mMinOrMaxJagArr = new Type[Sisuu + 1][];
        foreach (var EachPair in mLogDict) {
            long CurrBeki2 = EachPair.Key;
            long CurrSisuu = EachPair.Value;
            long CurrUB = pInitList.Count - 1 - CurrBeki2 + 1;
            mMinOrMaxJagArr[CurrSisuu] = new Type[CurrUB + 1];
        }

        // 長さ1の分を初期化
        for (int I = 0; I <= pInitList.Count - 1; I++) {
            mMinOrMaxJagArr[0][I] = pInitList[I];
        }

        // コンストラクタのヘルパメソッド
        ConstructorHelper();

        // 対応する2べき値[クエリの区間長]を設定
        SetBeki2Arr(pInitList.Count);
    }

    // コンストラクタ3(列挙が引数)
    internal SparseTable(IEnumerable<Type> pInitEnum, bool pIsMin)
    {
        mIsMin = pIsMin;

        Type[] InitArr = pInitEnum.ToArray();

        long Sisuu = 0;
        long Beki2 = 1;
        while (true) {
            mLogDict[Beki2] = Sisuu;
            Beki2 *= 2;

            if (Beki2 > InitArr.Length) {
                break;
            }
            Sisuu++;
        }

        mMinOrMaxJagArr = new Type[Sisuu + 1][];
        foreach (var EachPair in mLogDict) {
            long CurrBeki2 = EachPair.Key;
            long CurrSisuu = EachPair.Value;
            long CurrUB = InitArr.GetUpperBound(0) - CurrBeki2 + 1;
            mMinOrMaxJagArr[CurrSisuu] = new Type[CurrUB + 1];
        }

        // 長さ1の分を初期化
        for (int I = 0; I <= InitArr.GetUpperBound(0); I++) {
            mMinOrMaxJagArr[0][I] = InitArr[I];
        }

        // コンストラクタのヘルパメソッド
        ConstructorHelper();

        // 対応する2べき値[クエリの区間長]を設定
        SetBeki2Arr(InitArr.Length);
    }

    // 対応する2べき値[クエリの区間長]を設定
    private void SetBeki2Arr(long pDataLen)
    {
        long[] Beki2Arr = mLogDict.Keys.ToArray();

        mBeki2Arr = new long[pDataLen + 1];
        long CurrBeki2 = 1;
        for (long I = 0; I <= pDataLen; I++) {
            mBeki2Arr[I] = CurrBeki2;
            if (mLogDict.ContainsKey(I)) {
                CurrBeki2 = I;
            }
        }
    }

    // コンストラクタのヘルパメソッド
    private void ConstructorHelper()
    {
        for (long LoopLength = 2; LoopLength < long.MaxValue; LoopLength *= 2) {
            if (mLogDict.ContainsKey(LoopLength) == false) break;

            long HalfRange = LoopLength / 2;
            Type[] CurrArr = mMinOrMaxJagArr[mLogDict[HalfRange]];
            Type[] SendArr = mMinOrMaxJagArr[mLogDict[LoopLength]];
            for (long I = 0; I <= SendArr.GetUpperBound(0); I++) {
                long StaInd1 = I;
                long EndInd1 = I + HalfRange - 1;
                long StaInd2 = EndInd1 + 1;
                long EndInd2 = StaInd2 + HalfRange - 1;

                Type Val1 = CurrArr[I];
                Type Val2 = CurrArr[StaInd2];
                int CompResult = Val1.CompareTo(Val2);
                if (mIsMin) {
                    SendArr[I] = (CompResult >= 1 ? Val2 : Val1);
                }
                else {
                    SendArr[I] = (CompResult >= 1 ? Val1 : Val2);
                }
            }
        }
    }

    // 閉区間 [L,R]の最小値または最大値を求める
    internal Type Query(long pL, long pR)
    {
        // 区間を内包する2べき値を使用
        long RangeLen = pR - pL + 1;
        long Beki2 = mBeki2Arr[RangeLen];
        long Sisuu = mLogDict[Beki2];

        Type Val1 = mMinOrMaxJagArr[Sisuu][pL];
        Type Val2 = mMinOrMaxJagArr[Sisuu][pR - Beki2 + 1];
        int CompResult = Val1.CompareTo(Val2);

        if (mIsMin) {
            return (CompResult >= 1 ? Val2 : Val1);
        }
        return (CompResult >= 1 ? Val1 : Val2);
    }
}
#endregion


解説

オイラーツアーしてから、スパーステーブルを使用してます。
レベルの最小値を持つノードは、二分探索で探してます。