結果

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

vector<int> bfs(vector<vector<int>> &graph, int s) {
    int INF = numeric_limits<int>::max();
    vector<int> dist(graph.size(), INF);
    queue<int> q;
    dist[s] = 0, q.push(s);
    while(!q.empty()){
        int u = q.front(); q.pop();
        for(int v : graph[u]) if(dist[v] == INF)
            dist[v] = dist[u] + 1, q.push(v);
    }
    return dist;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N,M; cin >> N >> M;
    vector<vector<int>> G(N);
    rep(_,M) {
        int p,q; cin >> p >> q; p--; q--;
        G[p].push_back(q);
        G[q].push_back(p);
    }

    int Q; cin >> Q;
    rep(_,Q) {
        int a; cin >> a; a--;
        auto dist = bfs(G, a);
        int cnt = 0, ma = 0;
        rep(i,N) if(dist[i] < 2e9) {
            cnt++;
            ma = max(ma, dist[i]);
        }
        ma = (ma == 0 ? 0 : (int)ceil(log2(ma)));
        cout << cnt - 1 << " " << ma << "\n";
    }
}
0