結果

問題 No.812 Change of Class
ユーザー mhrb_minasemhrb_minase
提出日時 2019-04-12 22:54:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 4,000 ms
コード長 3,026 bytes
コンパイル時間 1,732 ms
コンパイル使用メモリ 177,524 KB
実行使用メモリ 10,108 KB
最終ジャッジ日時 2023-09-03 14:01:13
合計ジャッジ時間 7,519 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
8,352 KB
testcase_01 AC 10 ms
4,376 KB
testcase_02 AC 32 ms
5,972 KB
testcase_03 AC 67 ms
9,248 KB
testcase_04 AC 59 ms
8,516 KB
testcase_05 AC 174 ms
8,800 KB
testcase_06 AC 157 ms
7,480 KB
testcase_07 AC 6 ms
4,424 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 156 ms
9,392 KB
testcase_10 AC 166 ms
9,444 KB
testcase_11 AC 22 ms
7,208 KB
testcase_12 AC 113 ms
8,676 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 109 ms
7,252 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 96 ms
7,720 KB
testcase_18 AC 73 ms
6,480 KB
testcase_19 AC 84 ms
6,808 KB
testcase_20 AC 162 ms
9,124 KB
testcase_21 AC 71 ms
6,228 KB
testcase_22 AC 33 ms
4,428 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 10 ms
4,376 KB
testcase_25 AC 27 ms
4,376 KB
testcase_26 AC 138 ms
8,372 KB
testcase_27 AC 121 ms
7,764 KB
testcase_28 AC 83 ms
6,924 KB
testcase_29 AC 19 ms
4,376 KB
testcase_30 AC 100 ms
7,720 KB
testcase_31 AC 88 ms
6,672 KB
testcase_32 AC 40 ms
4,900 KB
testcase_33 AC 105 ms
8,160 KB
testcase_34 AC 9 ms
4,376 KB
testcase_35 AC 21 ms
4,376 KB
testcase_36 AC 8 ms
4,376 KB
testcase_37 AC 44 ms
5,384 KB
testcase_38 AC 9 ms
4,784 KB
testcase_39 AC 46 ms
5,468 KB
testcase_40 AC 13 ms
4,376 KB
testcase_41 AC 7 ms
4,480 KB
testcase_42 AC 91 ms
7,368 KB
testcase_43 AC 32 ms
6,164 KB
testcase_44 AC 68 ms
6,260 KB
testcase_45 AC 8 ms
4,376 KB
testcase_46 AC 30 ms
6,324 KB
testcase_47 AC 61 ms
6,276 KB
testcase_48 AC 73 ms
6,896 KB
testcase_49 AC 19 ms
4,380 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 23 ms
4,680 KB
testcase_52 AC 48 ms
6,644 KB
testcase_53 AC 13 ms
4,376 KB
testcase_54 AC 11 ms
4,376 KB
testcase_55 AC 45 ms
8,160 KB
testcase_56 AC 54 ms
9,016 KB
testcase_57 AC 56 ms
9,372 KB
testcase_58 AC 29 ms
6,344 KB
testcase_59 AC 64 ms
10,108 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = (1 << 30) - 1;
const long long LLINF = 1LL << 61;
const long long MOD = 1000000007LL;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

struct unionFind{
    vector<int> par;
    int siz;

    void init(int N){
        par.resize(N);
        fill(par.begin(), par.end(), -1);
        siz = N;
        return;
    }

    int root(int X){
        if(par[X] < 0){
            return X;
        }
        return par[X] = root(par[X]);
    }

    bool unite(int X, int Y){
        X = root(X);
        Y = root(Y);

        if(X == Y){
            return false;
        }

        if(par[X] < par[Y]){
            swap(X, Y);
        }

        par[Y] += par[X];
        par[X] = Y;
        --siz;

        return true;
    }

    bool same(int X, int Y){
        return root(X) == root(Y);
    }

    int size(int X){
        return -par[root(X)];
    }
};

template<typename T>
bool chmin(T &_a, T _b){
    if(_a > _b){
        _a = _b;
        return true;
    }else{
        return false;
    }
}

template<typename T>
bool chmax(T &_a, T _b){
    if(_a < _b){
        _a = _b;
        return true;
    }else{
        return false;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    unionFind uf;
    uf.init(n);
    vector< vector<int> > g(n);
    rep(i, m){
        int p, q;
        cin >> p >> q;
        --p;
        --q;
        uf.unite(p, q);
        g[p].emplace_back(q);
        g[q].emplace_back(p);
    }

    int q;
    cin >> q;
    rep(I, q){
        int a;
        cin >> a;
        --a;
        vector<int> cost(n, IINF);
        queue< P > que;
        que.push({a, 0});
        while(!que.empty()){
            P now = que.front();
            que.pop();
            if(!chmin(cost[now.first], now.second)){
                continue;
            }
            for(auto x : g[now.first]){
                if(cost[x] > now.second + 1){
                    que.push({x, now.second + 1});
                }
            }
        }

        int maxi = 0;
        rep(i, n){
            if(uf.same(i, a)){
                chmax(maxi, cost[i]);
            }
        }

        int ans1 = uf.size(a) - 1, ans2 = 0;
        if(maxi > 0){
            ans2 = ceil(log2(maxi));
        }

        cout << ans1 << " " << ans2 << endl;
    }

    return 0;
}
0