結果

問題 No.812 Change of Class
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-04-15 23:16:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 173 ms / 4,000 ms
コード長 1,277 bytes
コンパイル時間 864 ms
コンパイル使用メモリ 102,640 KB
最終ジャッジ日時 2025-01-07 02:14:51
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 60
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>

int main() {
    int n, m;
    std::cin >> n >> m;

    std::vector<std::vector<int>> nxts(n, std::vector<int>());
    for (int i = 0; i < m; i++) {
        int p, q;
        std::cin >> p >> q;
        p--;
        q--;
        nxts[p].push_back(q);
        nxts[q].push_back(p);
    }

    int q;
    std::cin >> q;

    for (int i = 0; i < q; i++) {
        int a;
        std::cin >> a;
        a--;

        int inf = 1000000007;
        std::vector<int> d(n, inf);
        d[a] = 0;
        std::queue<int> que;
        que.push(a);
        while(!que.empty()){
            int x = que.front();
            que.pop();
            for (auto&& y : nxts[x]) {
                if (d[x] + 1 < d[y]) {
                    d[y] = d[x] + 1;
                    que.push(y); 
                }
            }
        }

        int b = 0;
        int c = 0;
        for (auto&& v : d) {
            if (v != 0 && v != inf) {
                b++;
                c = std::max(c, v);
            }
        }

        if (c == 0) {
            std::cout << b << " " << c << std::endl;
        } else {
            std::cout << b << " " << std::ceil(std::log2(c)) << std::endl;
        }
    }
} 
0