結果

問題 No.812 Change of Class
ユーザー hamuhei4869hamuhei4869
提出日時 2019-03-20 18:10:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 992 ms / 4,000 ms
コード長 2,543 bytes
コンパイル時間 1,878 ms
コンパイル使用メモリ 162,192 KB
実行使用メモリ 20,004 KB
最終ジャッジ日時 2023-09-03 00:29:34
合計ジャッジ時間 26,402 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 309 ms
15,060 KB
testcase_01 AC 48 ms
4,824 KB
testcase_02 AC 166 ms
9,288 KB
testcase_03 AC 356 ms
16,640 KB
testcase_04 AC 301 ms
15,116 KB
testcase_05 AC 992 ms
19,104 KB
testcase_06 AC 837 ms
17,616 KB
testcase_07 AC 8 ms
4,568 KB
testcase_08 AC 4 ms
4,384 KB
testcase_09 AC 938 ms
18,720 KB
testcase_10 AC 952 ms
18,532 KB
testcase_11 AC 95 ms
8,480 KB
testcase_12 AC 661 ms
15,748 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 634 ms
13,148 KB
testcase_16 AC 38 ms
4,380 KB
testcase_17 AC 605 ms
15,744 KB
testcase_18 AC 440 ms
10,976 KB
testcase_19 AC 499 ms
12,448 KB
testcase_20 AC 946 ms
18,164 KB
testcase_21 AC 419 ms
10,752 KB
testcase_22 AC 180 ms
6,632 KB
testcase_23 AC 29 ms
4,376 KB
testcase_24 AC 49 ms
4,376 KB
testcase_25 AC 143 ms
6,016 KB
testcase_26 AC 786 ms
16,952 KB
testcase_27 AC 685 ms
15,280 KB
testcase_28 AC 494 ms
12,412 KB
testcase_29 AC 100 ms
5,004 KB
testcase_30 AC 622 ms
14,044 KB
testcase_31 AC 528 ms
11,936 KB
testcase_32 AC 235 ms
7,480 KB
testcase_33 AC 696 ms
15,408 KB
testcase_34 AC 45 ms
4,376 KB
testcase_35 AC 124 ms
5,620 KB
testcase_36 AC 47 ms
4,376 KB
testcase_37 AC 290 ms
7,996 KB
testcase_38 AC 29 ms
5,124 KB
testcase_39 AC 299 ms
7,996 KB
testcase_40 AC 75 ms
4,724 KB
testcase_41 AC 19 ms
4,896 KB
testcase_42 AC 643 ms
12,456 KB
testcase_43 AC 196 ms
9,152 KB
testcase_44 AC 438 ms
9,996 KB
testcase_45 AC 48 ms
4,376 KB
testcase_46 AC 183 ms
9,068 KB
testcase_47 AC 431 ms
10,076 KB
testcase_48 AC 478 ms
11,480 KB
testcase_49 AC 124 ms
5,420 KB
testcase_50 AC 7 ms
4,376 KB
testcase_51 AC 142 ms
6,148 KB
testcase_52 AC 300 ms
9,900 KB
testcase_53 AC 80 ms
4,904 KB
testcase_54 AC 68 ms
4,644 KB
testcase_55 AC 381 ms
14,496 KB
testcase_56 AC 554 ms
17,836 KB
testcase_57 AC 562 ms
18,972 KB
testcase_58 AC 276 ms
10,860 KB
testcase_59 AC 648 ms
20,004 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using LL = long long;
const LL LINF = 1e18;

class Edge{
public:
    int from,to;
    LL cost;
    Edge(int f, int t,LL c){
        from = f;
        to = t;
        cost = c;
    }
};

class UnionFind {
public:
    vector<int> data;
    UnionFind(int size) : data(size, -1) { }
    bool connect(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};


std::vector<LL> dijkstra(int s,int N,vector<Edge> E){
    std::vector<LL> ans(N,LINF);
    std::vector<std::vector<Edge>> Edges(N);
    for(auto e : E){
        Edges.at(e.from).push_back(e);
    }
    priority_queue<pair<LL,int>, std::vector<pair<LL,int>>, greater<pair<LL,int>>> que;
    ans.at(s)= 0;
    que.push(make_pair(0,s));
    while(!que.empty()){
        pair<LL,int> p = que.top();que.pop();
        if(ans.at(p.second) < p.first)continue;
        for(int i = 0;i < Edges.at(p.second).size();i++){
                if(ans.at(p.second) + Edges.at(p.second).at(i).cost < ans.at(Edges.at(p.second).at(i).to)){
                ans.at(Edges.at(p.second).at(i).to) = ans.at(p.second) + Edges.at(p.second).at(i).cost;
                que.push(make_pair(ans.at(Edges.at(p.second).at(i).to),Edges.at(p.second).at(i).to));
            }
        }
    }
    return ans;
}
int main(){
    int N,M;
    cin >> N >> M;
    UnionFind Uni(N);
    vector<Edge> Edges;
    for(int i = 0;i < M;i++){
        int p,q;cin >> p >> q;
        p--,q--;
        Edges.push_back(Edge(p,q,1));
        Edges.push_back(Edge(q,p,1));
        Uni.connect(p, q);
    }
    int Q;cin >> Q;
    for(int i = 0;i < Q;i++){
        int A;cin >> A;
        A--;
        vector<LL> dis = dijkstra(A, N, Edges);

        LL d = 0;
        for(int j = 0;j < N;j++){
            if(dis.at(j) != LINF)d = max(d, dis.at(j));
        }
        if(d == 1){
            cout<<Uni.size(A)-1<<" "<<0<<endl;
            continue;
        }
        d = (d-1)/2;
        int total = 1;
        int ans = 0;
        while(d >= 1){
            ans++;
            d -= total;
            total *= 2;
        }
        if(Uni.size(A) == 1)cout<<0<<" "<<0<<endl;
        else cout<<Uni.size(A)-1<<" "<<ans+1<<endl;
    }
}
0