結果

問題 No.812 Change of Class
ユーザー hamuhei4869hamuhei4869
提出日時 2019-03-20 18:15:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,333 ms / 4,000 ms
コード長 2,543 bytes
コンパイル時間 2,991 ms
コンパイル使用メモリ 162,416 KB
実行使用メモリ 20,288 KB
最終ジャッジ日時 2023-09-03 00:38:27
合計ジャッジ時間 30,024 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 315 ms
14,564 KB
testcase_01 AC 47 ms
4,852 KB
testcase_02 AC 173 ms
9,660 KB
testcase_03 AC 376 ms
16,864 KB
testcase_04 AC 328 ms
15,064 KB
testcase_05 AC 1,306 ms
18,556 KB
testcase_06 AC 1,078 ms
17,652 KB
testcase_07 AC 8 ms
4,648 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 1,211 ms
18,512 KB
testcase_10 AC 1,256 ms
18,536 KB
testcase_11 AC 99 ms
8,372 KB
testcase_12 AC 851 ms
16,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 721 ms
13,072 KB
testcase_16 AC 39 ms
4,380 KB
testcase_17 AC 722 ms
15,380 KB
testcase_18 AC 477 ms
10,924 KB
testcase_19 AC 613 ms
12,472 KB
testcase_20 AC 1,333 ms
18,284 KB
testcase_21 AC 436 ms
10,656 KB
testcase_22 AC 185 ms
6,584 KB
testcase_23 AC 31 ms
4,380 KB
testcase_24 AC 51 ms
4,380 KB
testcase_25 AC 144 ms
6,000 KB
testcase_26 AC 901 ms
16,080 KB
testcase_27 AC 764 ms
14,980 KB
testcase_28 AC 525 ms
12,228 KB
testcase_29 AC 106 ms
5,024 KB
testcase_30 AC 702 ms
14,528 KB
testcase_31 AC 567 ms
11,944 KB
testcase_32 AC 239 ms
7,596 KB
testcase_33 AC 918 ms
15,108 KB
testcase_34 AC 46 ms
4,376 KB
testcase_35 AC 128 ms
5,648 KB
testcase_36 AC 48 ms
4,380 KB
testcase_37 AC 312 ms
7,840 KB
testcase_38 AC 29 ms
5,144 KB
testcase_39 AC 318 ms
7,836 KB
testcase_40 AC 77 ms
4,656 KB
testcase_41 AC 20 ms
4,832 KB
testcase_42 AC 901 ms
12,700 KB
testcase_43 AC 208 ms
9,036 KB
testcase_44 AC 458 ms
9,956 KB
testcase_45 AC 49 ms
4,376 KB
testcase_46 AC 190 ms
8,884 KB
testcase_47 AC 503 ms
9,980 KB
testcase_48 AC 555 ms
11,424 KB
testcase_49 AC 130 ms
5,396 KB
testcase_50 AC 7 ms
4,380 KB
testcase_51 AC 147 ms
6,280 KB
testcase_52 AC 311 ms
9,968 KB
testcase_53 AC 82 ms
4,736 KB
testcase_54 AC 72 ms
4,540 KB
testcase_55 AC 406 ms
14,396 KB
testcase_56 AC 570 ms
18,100 KB
testcase_57 AC 606 ms
19,240 KB
testcase_58 AC 302 ms
10,992 KB
testcase_59 AC 688 ms
20,288 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 2 ms
4,384 KB
testcase_62 AC 2 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