結果
問題 | No.2325 Skill Tree |
ユーザー | tenten |
提出日時 | 2024-07-31 10:23:06 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,336 ms / 3,000 ms |
コード長 | 2,802 bytes |
コンパイル時間 | 2,382 ms |
コンパイル使用メモリ | 95,420 KB |
実行使用メモリ | 97,952 KB |
最終ジャッジ日時 | 2024-07-31 10:23:45 |
合計ジャッジ時間 | 36,685 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 66 ms
51,160 KB |
testcase_01 | AC | 67 ms
50,848 KB |
testcase_02 | AC | 68 ms
50,904 KB |
testcase_03 | AC | 66 ms
50,764 KB |
testcase_04 | AC | 71 ms
51,272 KB |
testcase_05 | AC | 66 ms
51,068 KB |
testcase_06 | AC | 69 ms
51,184 KB |
testcase_07 | AC | 415 ms
64,300 KB |
testcase_08 | AC | 417 ms
66,744 KB |
testcase_09 | AC | 463 ms
66,324 KB |
testcase_10 | AC | 550 ms
75,676 KB |
testcase_11 | AC | 536 ms
67,092 KB |
testcase_12 | AC | 818 ms
91,176 KB |
testcase_13 | AC | 833 ms
90,648 KB |
testcase_14 | AC | 815 ms
89,900 KB |
testcase_15 | AC | 872 ms
90,396 KB |
testcase_16 | AC | 820 ms
90,092 KB |
testcase_17 | AC | 768 ms
86,100 KB |
testcase_18 | AC | 873 ms
85,252 KB |
testcase_19 | AC | 815 ms
85,660 KB |
testcase_20 | AC | 769 ms
85,244 KB |
testcase_21 | AC | 783 ms
85,084 KB |
testcase_22 | AC | 916 ms
94,408 KB |
testcase_23 | AC | 887 ms
94,316 KB |
testcase_24 | AC | 776 ms
94,016 KB |
testcase_25 | AC | 805 ms
93,936 KB |
testcase_26 | AC | 871 ms
93,844 KB |
testcase_27 | AC | 1,129 ms
79,892 KB |
testcase_28 | AC | 1,174 ms
95,536 KB |
testcase_29 | AC | 1,336 ms
97,952 KB |
testcase_30 | AC | 1,256 ms
95,472 KB |
testcase_31 | AC | 1,200 ms
82,740 KB |
testcase_32 | AC | 1,039 ms
93,288 KB |
testcase_33 | AC | 968 ms
79,220 KB |
testcase_34 | AC | 1,197 ms
94,788 KB |
testcase_35 | AC | 1,205 ms
96,084 KB |
testcase_36 | AC | 1,072 ms
78,916 KB |
testcase_37 | AC | 1,058 ms
92,744 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } int[] levels = new int[n]; for (int i = 1; i < n; i++) { levels[i] = sc.nextInt(); int x = sc.nextInt() - 1; graph.get(x).add(i); } TreeMap<Integer, Integer> counts = new TreeMap<>(); Deque<Path> deq = new ArrayDeque<>(); deq.add(new Path(0, 1)); boolean[] visited = new boolean[n]; while (deq.size() > 0) { Path p = deq.poll(); p.value = Math.max(p.value, levels[p.idx]); levels[p.idx] = p.value; visited[p.idx] = true; counts.put(p.value, counts.getOrDefault(p.value, 0) + 1); for (int x : graph.get(p.idx)) { deq.add(new Path(x, p.value)); } } int current = 0; for (int x : counts.keySet()) { current += counts.get(x); counts.put(x, current); } int q = sc.nextInt(); String result = IntStream.range(0, q).mapToObj(i -> { int type = sc.nextInt(); int x = sc.nextInt(); if (type == 1) { return counts.floorEntry(x).getValue().toString(); } else { return visited[x - 1] ? String.valueOf(levels[x - 1]) : "-1"; } }).collect(Collectors.joining("\n")); System.out.println(result); } static class Path { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }