結果

問題 No.812 Change of Class
ユーザー sykwersykwer
提出日時 2019-04-12 21:59:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,800 bytes
コンパイル時間 1,088 ms
コンパイル使用メモリ 121,884 KB
実行使用メモリ 12,720 KB
最終ジャッジ日時 2023-09-03 12:45:03
合計ジャッジ時間 7,112 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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
5,244 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 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 6 ms
5,940 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 4 ms
5,340 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 65 ms
9,656 KB
testcase_56 AC 82 ms
10,960 KB
testcase_57 AC 81 ms
11,312 KB
testcase_58 AC 42 ms
7,504 KB
testcase_59 AC 91 ms
12,720 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 WA -
testcase_62 AC 1 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, dis / 2) << endl;
    }

    return 0;
}
0