結果

問題 No.2325 Skill Tree
ユーザー tentententen
提出日時 2023-05-30 14:10:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,392 ms / 3,000 ms
コード長 3,108 bytes
コンパイル時間 3,087 ms
コンパイル使用メモリ 94,620 KB
実行使用メモリ 83,732 KB
最終ジャッジ日時 2024-06-08 20:09:03
合計ジャッジ時間 40,933 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
36,788 KB
testcase_01 AC 58 ms
37,200 KB
testcase_02 AC 56 ms
37,300 KB
testcase_03 AC 58 ms
37,220 KB
testcase_04 AC 57 ms
37,172 KB
testcase_05 AC 57 ms
37,192 KB
testcase_06 AC 55 ms
37,140 KB
testcase_07 AC 421 ms
51,244 KB
testcase_08 AC 445 ms
54,592 KB
testcase_09 AC 470 ms
53,784 KB
testcase_10 AC 625 ms
63,448 KB
testcase_11 AC 600 ms
62,904 KB
testcase_12 AC 964 ms
74,440 KB
testcase_13 AC 972 ms
75,696 KB
testcase_14 AC 989 ms
78,116 KB
testcase_15 AC 963 ms
75,616 KB
testcase_16 AC 1,032 ms
74,548 KB
testcase_17 AC 1,028 ms
76,616 KB
testcase_18 AC 1,000 ms
74,396 KB
testcase_19 AC 967 ms
74,284 KB
testcase_20 AC 1,032 ms
74,028 KB
testcase_21 AC 877 ms
74,096 KB
testcase_22 AC 1,101 ms
74,840 KB
testcase_23 AC 985 ms
73,800 KB
testcase_24 AC 1,091 ms
74,824 KB
testcase_25 AC 979 ms
83,732 KB
testcase_26 AC 1,010 ms
83,480 KB
testcase_27 AC 1,375 ms
81,424 KB
testcase_28 AC 1,252 ms
80,928 KB
testcase_29 AC 1,238 ms
81,288 KB
testcase_30 AC 1,233 ms
71,596 KB
testcase_31 AC 1,194 ms
70,040 KB
testcase_32 AC 1,119 ms
78,092 KB
testcase_33 AC 1,293 ms
80,468 KB
testcase_34 AC 1,359 ms
81,024 KB
testcase_35 AC 1,333 ms
76,204 KB
testcase_36 AC 1,232 ms
75,296 KB
testcase_37 AC 1,392 ms
78,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        ArrayList<ArrayList<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);
        }
        int[] needs = new int[n];
        int[] tmp = new int[n];
        Arrays.fill(needs, -1);
        Arrays.fill(tmp, Integer.MAX_VALUE);
        ArrayDeque<Path> queue = new ArrayDeque<>();
        queue.add(new Path(0, 0));
        while (queue.size() > 0) {
            Path p = queue.poll();
            p.value = Math.max(p.value, levels[p.idx]);
            needs[p.idx] = p.value;
            tmp[p.idx] = p.value;
            for (int x : graph.get(p.idx)) {
                queue.add(new Path(x, p.value));
            }
        }
        TreeMap<Integer, Integer> counts = new TreeMap<>();
        Arrays.sort(tmp);
        for (int i = 0; i < n; i++) {
            counts.put(tmp[i], i + 1);
        }
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (q-- > 0) {
            int type = sc.nextInt();
            int v = sc.nextInt();
            if (type == 1) {
                sb.append(counts.floorEntry(v).getValue()).append("\n");
            } else {
                sb.append(needs[v - 1]).append("\n");
            }
        }
        System.out.print(sb);
    }
    
    static class Path {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0