結果

問題 No.778 クリスマスツリー
ユーザー tentententen
提出日時 2021-01-28 13:33:47
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,117 bytes
コンパイル時間 3,639 ms
コンパイル使用メモリ 80,508 KB
実行使用メモリ 56,204 KB
最終ジャッジ日時 2023-09-08 04:34:58
合計ジャッジ時間 8,508 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
56,084 KB
testcase_01 AC 125 ms
55,716 KB
testcase_02 AC 123 ms
55,968 KB
testcase_03 AC 124 ms
56,028 KB
testcase_04 AC 123 ms
56,204 KB
testcase_05 AC 124 ms
55,564 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 1; i < n; i++) {
            graph.get(sc.nextInt()).add(i);
        }
        ArrayList<Integer> list = new ArrayList<>();
        list.add(Integer.MIN_VALUE);
        list.add(Integer.MAX_VALUE);
        System.out.println(search(0, list));
    }
    
    static int search(int idx, ArrayList<Integer> list) {
        int left = 0;
        int right = list.size();
        while (right - left > 1) {
            int m = (left + right) / 2;
            if (list.get(m) >= idx) {
                right = m;
            } else {
                left = m;
            }
        }
        list.add(right, idx);
        int sum = right - 1;
        for (int x : graph.get(idx)) {
            sum += search(x, list);
        }
        list.remove(right);
        return sum;
    }
}
0