結果

問題 No.812 Change of Class
ユーザー hamuhei4869hamuhei4869
提出日時 2019-03-21 15:03:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,406 ms / 4,000 ms
コード長 2,282 bytes
コンパイル時間 2,979 ms
コンパイル使用メモリ 161,824 KB
実行使用メモリ 19,956 KB
最終ジャッジ日時 2023-09-03 00:48:59
合計ジャッジ時間 30,180 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 315 ms
15,124 KB
testcase_01 AC 47 ms
5,068 KB
testcase_02 AC 168 ms
9,812 KB
testcase_03 AC 372 ms
16,616 KB
testcase_04 AC 327 ms
15,492 KB
testcase_05 AC 1,406 ms
18,648 KB
testcase_06 AC 1,111 ms
18,100 KB
testcase_07 AC 8 ms
4,524 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 1,379 ms
18,624 KB
testcase_10 AC 1,382 ms
18,728 KB
testcase_11 AC 100 ms
8,660 KB
testcase_12 AC 769 ms
16,404 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 685 ms
13,500 KB
testcase_16 AC 38 ms
4,380 KB
testcase_17 AC 695 ms
15,700 KB
testcase_18 AC 469 ms
11,016 KB
testcase_19 AC 563 ms
12,544 KB
testcase_20 AC 1,334 ms
18,024 KB
testcase_21 AC 464 ms
10,888 KB
testcase_22 AC 187 ms
6,688 KB
testcase_23 AC 31 ms
4,380 KB
testcase_24 AC 51 ms
4,504 KB
testcase_25 AC 146 ms
6,116 KB
testcase_26 AC 1,111 ms
15,984 KB
testcase_27 AC 892 ms
14,580 KB
testcase_28 AC 580 ms
12,516 KB
testcase_29 AC 103 ms
5,176 KB
testcase_30 AC 739 ms
14,388 KB
testcase_31 AC 575 ms
12,108 KB
testcase_32 AC 240 ms
7,620 KB
testcase_33 AC 792 ms
15,308 KB
testcase_34 AC 45 ms
4,384 KB
testcase_35 AC 125 ms
5,756 KB
testcase_36 AC 47 ms
4,384 KB
testcase_37 AC 296 ms
8,120 KB
testcase_38 AC 30 ms
5,152 KB
testcase_39 AC 306 ms
8,060 KB
testcase_40 AC 77 ms
5,000 KB
testcase_41 AC 20 ms
5,052 KB
testcase_42 AC 789 ms
12,640 KB
testcase_43 AC 213 ms
9,020 KB
testcase_44 AC 519 ms
10,040 KB
testcase_45 AC 48 ms
4,376 KB
testcase_46 AC 197 ms
9,220 KB
testcase_47 AC 485 ms
10,072 KB
testcase_48 AC 530 ms
11,340 KB
testcase_49 AC 127 ms
5,728 KB
testcase_50 AC 7 ms
4,384 KB
testcase_51 AC 145 ms
6,364 KB
testcase_52 AC 318 ms
10,040 KB
testcase_53 AC 82 ms
4,928 KB
testcase_54 AC 71 ms
4,780 KB
testcase_55 AC 395 ms
14,368 KB
testcase_56 AC 553 ms
18,708 KB
testcase_57 AC 591 ms
18,672 KB
testcase_58 AC 288 ms
11,012 KB
testcase_59 AC 667 ms
19,956 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 1 ms
4,380 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));
        }
        int ans = 0;
        if(d != 0)ans = ceil(log2(d));
        cout << Uni.size(A)-1<<" "<<ans<<endl;
    }
}
0