結果

問題 No.812 Change of Class
ユーザー kyort0nkyort0n
提出日時 2019-04-12 21:35:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 251 ms / 4,000 ms
コード長 2,284 bytes
コンパイル時間 1,842 ms
コンパイル使用メモリ 176,292 KB
実行使用メモリ 11,256 KB
最終ジャッジ日時 2023-09-03 00:57:00
合計ジャッジ時間 9,227 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
9,776 KB
testcase_01 AC 16 ms
7,344 KB
testcase_02 AC 43 ms
8,724 KB
testcase_03 AC 88 ms
10,124 KB
testcase_04 AC 78 ms
9,900 KB
testcase_05 AC 251 ms
9,904 KB
testcase_06 AC 240 ms
9,696 KB
testcase_07 AC 5 ms
6,788 KB
testcase_08 AC 5 ms
7,016 KB
testcase_09 AC 217 ms
10,424 KB
testcase_10 AC 219 ms
10,380 KB
testcase_11 AC 18 ms
8,052 KB
testcase_12 AC 130 ms
10,020 KB
testcase_13 AC 4 ms
6,960 KB
testcase_14 AC 5 ms
6,864 KB
testcase_15 AC 151 ms
9,192 KB
testcase_16 AC 13 ms
7,384 KB
testcase_17 AC 140 ms
9,456 KB
testcase_18 AC 104 ms
8,540 KB
testcase_19 AC 122 ms
9,060 KB
testcase_20 AC 240 ms
10,132 KB
testcase_21 AC 100 ms
8,564 KB
testcase_22 AC 46 ms
7,868 KB
testcase_23 AC 12 ms
6,964 KB
testcase_24 AC 16 ms
7,256 KB
testcase_25 AC 38 ms
7,608 KB
testcase_26 AC 184 ms
9,704 KB
testcase_27 AC 161 ms
9,332 KB
testcase_28 AC 116 ms
8,984 KB
testcase_29 AC 29 ms
7,260 KB
testcase_30 AC 147 ms
9,112 KB
testcase_31 AC 132 ms
8,940 KB
testcase_32 AC 60 ms
8,068 KB
testcase_33 AC 155 ms
9,388 KB
testcase_34 AC 15 ms
7,400 KB
testcase_35 AC 25 ms
7,640 KB
testcase_36 AC 13 ms
7,264 KB
testcase_37 AC 57 ms
8,264 KB
testcase_38 AC 9 ms
7,260 KB
testcase_39 AC 60 ms
8,120 KB
testcase_40 AC 18 ms
7,324 KB
testcase_41 AC 7 ms
7,280 KB
testcase_42 AC 120 ms
9,048 KB
testcase_43 AC 36 ms
8,604 KB
testcase_44 AC 83 ms
8,580 KB
testcase_45 AC 13 ms
7,512 KB
testcase_46 AC 35 ms
8,332 KB
testcase_47 AC 88 ms
8,544 KB
testcase_48 AC 95 ms
8,892 KB
testcase_49 AC 26 ms
7,512 KB
testcase_50 AC 5 ms
6,876 KB
testcase_51 AC 29 ms
8,052 KB
testcase_52 AC 56 ms
8,632 KB
testcase_53 AC 19 ms
7,224 KB
testcase_54 AC 16 ms
7,324 KB
testcase_55 AC 54 ms
9,820 KB
testcase_56 AC 61 ms
10,324 KB
testcase_57 AC 66 ms
10,396 KB
testcase_58 AC 36 ms
8,880 KB
testcase_59 AC 75 ms
11,256 KB
testcase_60 AC 4 ms
6,760 KB
testcase_61 AC 5 ms
6,772 KB
testcase_62 AC 4 ms
7,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> Size;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        Size[x] += Size[y];
        return true;
    }

    ll size(int x){
        return Size[root(x)];
    }
};
vector<int> pathes[100500];

int dist[100050];

int main() {
    //cout.precision(10);
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    UnionFind uni(100050);
    int N, M;
    cin >> N >> M;
    for(int i = 1; i <= M; i++) {
        int a, b;
        cin >> a >> b;
        uni.merge(a, b);
        pathes[a].push_back(b);
        pathes[b].push_back(a);
    }
    int Q;
    cin >> Q;
    while(Q--) {
        int A;
        cin >> A;
        for(int i = 1; i <= N; i++) dist[i] = INF;
        dist[A] = 0;
        queue<int> search;
        search.push(A);
        int maxi = 0;
        while(!search.empty()) {
            int from = search.front();
            maxi = max(maxi, dist[from]);
            search.pop();
            for(int i = 0; i < pathes[from].size(); i++) {
                int to = pathes[from][i];
                if(dist[to] <= dist[from]) continue;
                dist[to] = dist[from] + 1;
                search.push(to);
            }
        }
        int day = 0;
        //cerr << maxi << endl;
        while(maxi >= 2) {
            maxi = (maxi + 1) / 2;
            day++;
        }
        cout << uni.size(A) - 1 << " ";
        cout << day << endl;
    }
    return 0;
}
0