結果

問題 No.812 Change of Class
ユーザー tenten
提出日時 2022-08-12 11:05:27
言語 Java
(openjdk 23)
結果
AC  
実行時間 2,291 ms / 4,000 ms
コード長 2,792 bytes
コンパイル時間 2,415 ms
コンパイル使用メモリ 80,844 KB
実行使用メモリ 89,112 KB
最終ジャッジ日時 2024-09-22 14:30:53
合計ジャッジ時間 61,246 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 60
権限があれば一括ダウンロードができます

ソースコード

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