結果

問題 No.812 Change of Class
ユーザー sykwersykwer
提出日時 2019-04-12 22:10:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 225 ms / 4,000 ms
コード長 2,814 bytes
コンパイル時間 1,447 ms
コンパイル使用メモリ 121,464 KB
実行使用メモリ 12,644 KB
最終ジャッジ日時 2023-09-03 12:41:12
合計ジャッジ時間 8,589 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
10,104 KB
testcase_01 AC 13 ms
4,588 KB
testcase_02 AC 45 ms
6,984 KB
testcase_03 AC 97 ms
11,340 KB
testcase_04 AC 86 ms
10,368 KB
testcase_05 AC 225 ms
11,484 KB
testcase_06 AC 193 ms
10,076 KB
testcase_07 AC 4 ms
5,496 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 204 ms
12,520 KB
testcase_10 AC 222 ms
12,508 KB
testcase_11 AC 20 ms
9,508 KB
testcase_12 AC 145 ms
11,776 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 138 ms
9,068 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 129 ms
9,684 KB
testcase_18 AC 97 ms
7,972 KB
testcase_19 AC 114 ms
8,552 KB
testcase_20 AC 225 ms
12,292 KB
testcase_21 AC 92 ms
7,788 KB
testcase_22 AC 41 ms
5,468 KB
testcase_23 AC 7 ms
4,384 KB
testcase_24 AC 11 ms
4,380 KB
testcase_25 AC 32 ms
4,912 KB
testcase_26 AC 164 ms
10,772 KB
testcase_27 AC 146 ms
9,804 KB
testcase_28 AC 107 ms
8,492 KB
testcase_29 AC 22 ms
4,888 KB
testcase_30 AC 135 ms
9,496 KB
testcase_31 AC 119 ms
8,376 KB
testcase_32 AC 52 ms
6,108 KB
testcase_33 AC 149 ms
10,284 KB
testcase_34 AC 10 ms
4,376 KB
testcase_35 AC 26 ms
5,012 KB
testcase_36 AC 10 ms
4,380 KB
testcase_37 AC 64 ms
6,400 KB
testcase_38 AC 7 ms
5,896 KB
testcase_39 AC 65 ms
6,244 KB
testcase_40 AC 16 ms
4,652 KB
testcase_41 AC 6 ms
5,564 KB
testcase_42 AC 134 ms
9,080 KB
testcase_43 AC 38 ms
7,880 KB
testcase_44 AC 97 ms
7,536 KB
testcase_45 AC 10 ms
4,380 KB
testcase_46 AC 34 ms
7,676 KB
testcase_47 AC 96 ms
7,712 KB
testcase_48 AC 106 ms
8,912 KB
testcase_49 AC 26 ms
5,136 KB
testcase_50 AC 3 ms
4,380 KB
testcase_51 AC 29 ms
5,320 KB
testcase_52 AC 61 ms
8,000 KB
testcase_53 AC 17 ms
4,576 KB
testcase_54 AC 14 ms
4,376 KB
testcase_55 AC 72 ms
9,696 KB
testcase_56 AC 85 ms
11,092 KB
testcase_57 AC 91 ms
11,432 KB
testcase_58 AC 47 ms
7,684 KB
testcase_59 AC 102 ms
12,644 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>
#include <cstring>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <stack>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define int ll
#define P pair<ll, ll>

/* ---------- Constants  */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

class UnionFind {
public:
    vector<int> par, rank;
    vector<int> forest_size;
    int forest_cnt;
    int N;

    UnionFind(int n) : forest_cnt(n), N(N) {
        par = vector<int>(n);
        iota(par.begin(), par.end(), 0);
        rank = vector<int>(n, 0);
        forest_size.resize(n, 1);
    }

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

    void unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;

        int fsz = forest_size[x] + forest_size[y];
        if (rank[x] < rank[y]) {
            par[x] = y;
            forest_size[y] = fsz;
        } else {
            par[y] = x;
            forest_size[x] = fsz;
            if (rank[x] == rank[y]) rank[x]++;
        }
        forest_cnt--;
    }

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

    int get_forest_size(int x) {
        return forest_size[root(x)];
    }
};

signed main() {
    int N, M;
    cin >> N >> M;
    vector<int> table[N];
    UnionFind uf = UnionFind(N);

    for (int i = 0; i < M; i++) {
        int s, t;
        cin >> s >> t;
        s--; t--;
        table[s].push_back(t);
        table[t].push_back(s);
        uf.unite(s, t);
    }

    int Q;
    cin >> Q;
    for (int q = 0; q < Q; q++) {
        int a;
        cin >> a;
        a--;

        vector<bool> visited(N, false);
        visited[a] = true;
        queue<int> que;
        que.push(a);
        int dis = 0;
        vector<int> dis_v(N, 0);
        while (!que.empty()) {
            int node = que.front(); que.pop();
            for (int next : table[node]) {
                if (visited[next]) continue;
                dis = max(dis, dis_v[node] + 1);
                dis_v[next] = dis_v[node] + 1;
                que.push(next);
                visited[next] = true;
            }
        }

        cout << uf.get_forest_size(a) - 1 << " " << max(0LL, (int) ceil(log2(dis))) << endl;
    }

    return 0;
}
0