結果

問題 No.392 2分木をたどれ
ユーザー 14番14番
提出日時 2016-07-16 05:27:11
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 2,724 bytes
コンパイル時間 4,272 ms
コンパイル使用メモリ 106,400 KB
実行使用メモリ 20,740 KB
最終ジャッジ日時 2023-08-05 15:53:23
合計ジャッジ時間 2,970 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
20,740 KB
testcase_01 AC 69 ms
20,556 KB
testcase_02 AC 68 ms
20,612 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        this.SetTree();
        int queryCount = int.Parse(Reader.ReadLine());

        for(int i=0; i<queryCount; i++) {
            int idx = int.Parse(Reader.ReadLine());
            Console.WriteLine(this.GetAns(idx));
        }

    }

    private String GetAns(int idx) {
        TreeNode current = this.NodeList[idx];
        if(current.Parent == null) {
            return string.Empty;
        }
        TreeNode parent = current.Parent;
        if(parent.Left.ID == idx) {
            return this.GetAns(parent.ID) + "L";
        } else {
            return this.GetAns(parent.ID) + "R";
        }
    }

    private void SetTree() {
        int max = 4094;
        this.NodeList = new TreeNode[max+1];

        int idx = 1;
        Queue<int> q = new Queue<int>();
        this.Root = new TreeNode(0);
        this.NodeList[0] = this.Root;
        q.Enqueue(0);
        while (q.Count > 0)
        {
            int next = q.Dequeue();
            this.NodeList[idx] = this.NodeList[next].AddChild(new TreeNode(idx));
            q.Enqueue(idx);
            idx++;
            if(idx > max) {
                break;
            }
            this.NodeList[idx] = this.NodeList[next].AddChild(new TreeNode(idx));
            q.Enqueue(idx);
            idx++;
            if(idx > max) {
                break;
            }
        }
    }

    public TreeNode[] NodeList;
    public TreeNode Root;

    public class TreeNode {
        public TreeNode Parent;
        public TreeNode Left;
        public TreeNode Right;

        public int ID;

        public TreeNode(int id) {
            this.ID = id;
        }

        public TreeNode AddChild(TreeNode cld) {
            if(this.Left == null) {
                this.Left = cld;
            } else {
                this.Right = cld;
            }
            cld.Parent = this;
            return cld;
        }
    }

    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


2
21
14



 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }

    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0