結果

問題 No.812 Change of Class
ユーザー hamuhei4869hamuhei4869
提出日時 2019-03-21 00:07:58
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,455 bytes
コンパイル時間 1,593 ms
コンパイル使用メモリ 162,980 KB
実行使用メモリ 20,724 KB
最終ジャッジ日時 2023-09-03 00:43:23
合計ジャッジ時間 27,517 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 317 ms
14,828 KB
testcase_01 AC 48 ms
4,892 KB
testcase_02 AC 169 ms
9,652 KB
testcase_03 AC 380 ms
17,624 KB
testcase_04 AC 331 ms
15,152 KB
testcase_05 AC 1,256 ms
18,816 KB
testcase_06 AC 996 ms
17,636 KB
testcase_07 AC 7 ms
4,604 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 WA -
testcase_10 AC 1,221 ms
18,896 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 582 ms
12,436 KB
testcase_20 WA -
testcase_21 AC 436 ms
10,876 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 970 ms
16,668 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 102 ms
5,112 KB
testcase_30 AC 729 ms
13,980 KB
testcase_31 AC 610 ms
12,088 KB
testcase_32 AC 233 ms
7,816 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 293 ms
7,964 KB
testcase_38 WA -
testcase_39 AC 300 ms
8,128 KB
testcase_40 WA -
testcase_41 AC 20 ms
5,016 KB
testcase_42 AC 732 ms
12,792 KB
testcase_43 WA -
testcase_44 AC 458 ms
10,156 KB
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 468 ms
10,176 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 AC 1 ms
4,376 KB
testcase_61 WA -
testcase_62 WA -
権限があれば一括ダウンロードができます

ソースコード

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 = ceil(log2(d));
        if(Uni.size(A) == 1)cout<<0<<" "<<0<<endl;
        else cout<<Uni.size(A)-1<<" "<<ans+1<<endl;
    }
}
0