結果

問題 No.778 クリスマスツリー
コンテスト
ユーザー tenten
提出日時 2021-01-28 13:33:47
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
TLE  
実行時間 -
コード長 1,117 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,446 ms
コンパイル使用メモリ 83,396 KB
実行使用メモリ 233,700 KB
最終ジャッジ日時 2026-03-12 08:10:18
合計ジャッジ時間 14,915 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 2 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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