結果
問題 |
No.2325 Skill Tree
|
ユーザー |
![]() |
提出日時 | 2024-07-31 10:23:06 |
言語 | Java (openjdk 23) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 36 |
ソースコード
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(); } } }