結果

問題 No.812 Change of Class
ユーザー tentententen
提出日時 2022-08-12 11:05:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,159 ms / 4,000 ms
コード長 2,792 bytes
コンパイル時間 3,246 ms
コンパイル使用メモリ 80,412 KB
実行使用メモリ 93,204 KB
最終ジャッジ日時 2023-10-23 20:55:17
合計ジャッジ時間 60,018 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 809 ms
78,808 KB
testcase_01 AC 351 ms
62,160 KB
testcase_02 AC 654 ms
67,972 KB
testcase_03 AC 992 ms
85,612 KB
testcase_04 AC 802 ms
79,372 KB
testcase_05 AC 1,892 ms
81,660 KB
testcase_06 AC 1,770 ms
85,572 KB
testcase_07 AC 104 ms
56,104 KB
testcase_08 AC 91 ms
55,468 KB
testcase_09 AC 1,673 ms
83,340 KB
testcase_10 AC 1,669 ms
83,976 KB
testcase_11 AC 241 ms
70,204 KB
testcase_12 AC 1,264 ms
80,540 KB
testcase_13 AC 53 ms
53,596 KB
testcase_14 AC 53 ms
53,584 KB
testcase_15 AC 1,388 ms
77,108 KB
testcase_16 AC 255 ms
63,800 KB
testcase_17 AC 1,260 ms
77,392 KB
testcase_18 AC 964 ms
68,524 KB
testcase_19 AC 1,564 ms
73,316 KB
testcase_20 AC 2,159 ms
85,268 KB
testcase_21 AC 906 ms
66,300 KB
testcase_22 AC 600 ms
65,600 KB
testcase_23 AC 233 ms
60,340 KB
testcase_24 AC 289 ms
64,184 KB
testcase_25 AC 572 ms
65,364 KB
testcase_26 AC 1,436 ms
77,584 KB
testcase_27 AC 1,489 ms
78,192 KB
testcase_28 AC 1,066 ms
74,860 KB
testcase_29 AC 487 ms
62,988 KB
testcase_30 AC 1,227 ms
76,644 KB
testcase_31 AC 1,095 ms
72,624 KB
testcase_32 AC 627 ms
64,984 KB
testcase_33 AC 1,279 ms
77,252 KB
testcase_34 AC 266 ms
64,172 KB
testcase_35 AC 469 ms
65,008 KB
testcase_36 AC 292 ms
64,804 KB
testcase_37 AC 803 ms
66,884 KB
testcase_38 AC 168 ms
59,424 KB
testcase_39 AC 797 ms
66,404 KB
testcase_40 AC 364 ms
62,672 KB
testcase_41 AC 153 ms
58,952 KB
testcase_42 AC 1,333 ms
73,292 KB
testcase_43 AC 392 ms
66,468 KB
testcase_44 AC 1,018 ms
66,616 KB
testcase_45 AC 306 ms
64,248 KB
testcase_46 AC 368 ms
66,256 KB
testcase_47 AC 1,198 ms
66,552 KB
testcase_48 AC 1,149 ms
69,104 KB
testcase_49 AC 574 ms
65,656 KB
testcase_50 AC 146 ms
59,120 KB
testcase_51 AC 485 ms
65,544 KB
testcase_52 AC 626 ms
66,376 KB
testcase_53 AC 458 ms
63,208 KB
testcase_54 AC 410 ms
63,108 KB
testcase_55 AC 1,105 ms
81,624 KB
testcase_56 AC 1,296 ms
92,340 KB
testcase_57 AC 1,145 ms
88,112 KB
testcase_58 AC 921 ms
77,892 KB
testcase_59 AC 1,406 ms
93,204 KB
testcase_60 AC 53 ms
51,664 KB
testcase_61 AC 52 ms
53,596 KB
testcase_62 AC 53 ms
53,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        StringBuilder sb = new StringBuilder();
        int q = sc.nextInt();
        PriorityQueue<Path> queue = new PriorityQueue<>();
        int[] days = new int[n];
        while (q-- > 0) {
            int x = sc.nextInt() - 1;
            Arrays.fill(days, Integer.MAX_VALUE);
            queue.add(new Path(x, 0));
            while (queue.size() > 0) {
                Path p = queue.poll();
                if (days[p.idx] <= p.value) {
                    continue;
                }
                days[p.idx] = p.value;
                for (int y : graph.get(p.idx)) {
                    queue.add(new Path(y, p.value + 1));
                }
            }
            int count = -1;
            int max = 0;
            for (int y : days) {
                if (y < Integer.MAX_VALUE) {
                    count++;
                    max = Math.max(max, y);
                }
            }
            sb.append(count).append(" ").append(getDay(max)).append("\n");
        }
        System.out.print(sb);
    }
    
    static int getDay(int d) {
        int day = 0;
        int base = 1;
        while (d > base) {
            day++;
            base *= 2;
        }
        return day;
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0