結果

問題 No.812 Change of Class
ユーザー hamuhei4869hamuhei4869
提出日時 2019-03-18 17:52:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,277 bytes
コンパイル時間 1,759 ms
コンパイル使用メモリ 181,848 KB
実行使用メモリ 20,160 KB
最終ジャッジ日時 2023-09-02 23:01:37
合計ジャッジ時間 26,823 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 8 ms
4,612 KB
testcase_08 AC 4 ms
4,368 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
4,372 KB
testcase_14 AC 2 ms
4,372 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 22 ms
4,760 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 AC 361 ms
14,596 KB
testcase_56 AC 487 ms
18,212 KB
testcase_57 AC 522 ms
19,264 KB
testcase_58 AC 261 ms
10,896 KB
testcase_59 AC 585 ms
20,160 KB
testcase_60 AC 2 ms
4,372 KB
testcase_61 AC 2 ms
4,372 KB
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(Uni.size(A) == 1)cout<<0<<" "<<0<<endl;
        else cout<<Uni.size(A)-1<<" "<<d-1<<endl;
    }
}
0