結果

問題 No.812 Change of Class
ユーザー mhrb_minasemhrb_minase
提出日時 2019-04-12 22:21:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,151 bytes
コンパイル時間 1,467 ms
コンパイル使用メモリ 177,184 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2023-09-03 13:13:41
合計ジャッジ時間 6,772 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
4,384 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 20 ms
6,980 KB
testcase_12 WA -
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 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 AC 8 ms
4,652 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 7 ms
4,424 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 41 ms
8,000 KB
testcase_56 AC 50 ms
8,892 KB
testcase_57 AC 52 ms
9,344 KB
testcase_58 AC 27 ms
6,240 KB
testcase_59 AC 59 ms
9,984 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 1 ms
4,380 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 <= 1){
            ans2 = 0;
        }else if(maxi <= 2){
            ans2 = 1;
        }else if(maxi <= 4){
            ans2 = 2;
        }else{
            ans2 = 3;
        }

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

    return 0;
}
0