結果
| 問題 |
No.778 クリスマスツリー
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-01-28 13:33:47 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,117 bytes |
| コンパイル時間 | 3,500 ms |
| コンパイル使用メモリ | 79,500 KB |
| 実行使用メモリ | 127,240 KB |
| 最終ジャッジ日時 | 2024-06-25 21:50:10 |
| 合計ジャッジ時間 | 8,358 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 3 TLE * 1 -- * 8 |
ソースコード
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;
}
}
tenten