結果

問題 No.812 Change of Class
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-11-12 21:50:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 152 ms / 4,000 ms
コード長 6,364 bytes
コンパイル時間 1,293 ms
コンパイル使用メモリ 119,248 KB
実行使用メモリ 10,704 KB
最終ジャッジ日時 2023-09-03 16:01:40
合計ジャッジ時間 6,177 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
8,860 KB
testcase_01 AC 9 ms
4,376 KB
testcase_02 AC 26 ms
6,192 KB
testcase_03 AC 54 ms
9,892 KB
testcase_04 AC 48 ms
8,892 KB
testcase_05 AC 152 ms
9,232 KB
testcase_06 AC 146 ms
7,904 KB
testcase_07 AC 4 ms
4,600 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 129 ms
9,900 KB
testcase_10 AC 135 ms
9,940 KB
testcase_11 AC 14 ms
7,820 KB
testcase_12 AC 78 ms
9,488 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 93 ms
7,560 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 83 ms
7,956 KB
testcase_18 AC 65 ms
6,484 KB
testcase_19 AC 75 ms
7,080 KB
testcase_20 AC 140 ms
9,776 KB
testcase_21 AC 65 ms
6,408 KB
testcase_22 AC 28 ms
4,612 KB
testcase_23 AC 6 ms
4,376 KB
testcase_24 AC 9 ms
4,376 KB
testcase_25 AC 22 ms
4,452 KB
testcase_26 AC 112 ms
8,720 KB
testcase_27 AC 100 ms
8,108 KB
testcase_28 AC 70 ms
6,996 KB
testcase_29 AC 18 ms
4,376 KB
testcase_30 AC 87 ms
7,896 KB
testcase_31 AC 77 ms
6,916 KB
testcase_32 AC 34 ms
5,088 KB
testcase_33 AC 90 ms
8,456 KB
testcase_34 AC 8 ms
4,376 KB
testcase_35 AC 15 ms
4,380 KB
testcase_36 AC 7 ms
4,380 KB
testcase_37 AC 40 ms
5,424 KB
testcase_38 AC 6 ms
5,248 KB
testcase_39 AC 41 ms
5,388 KB
testcase_40 AC 11 ms
4,380 KB
testcase_41 AC 6 ms
4,716 KB
testcase_42 AC 86 ms
7,596 KB
testcase_43 AC 19 ms
6,924 KB
testcase_44 AC 58 ms
6,436 KB
testcase_45 AC 8 ms
4,380 KB
testcase_46 AC 19 ms
6,488 KB
testcase_47 AC 60 ms
6,500 KB
testcase_48 AC 64 ms
7,248 KB
testcase_49 AC 16 ms
4,380 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 18 ms
4,608 KB
testcase_52 AC 36 ms
6,764 KB
testcase_53 AC 11 ms
4,380 KB
testcase_54 AC 10 ms
4,376 KB
testcase_55 AC 39 ms
8,492 KB
testcase_56 AC 45 ms
9,520 KB
testcase_57 AC 50 ms
9,892 KB
testcase_58 AC 26 ms
6,544 KB
testcase_59 AC 56 ms
10,704 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <cassert>
#include <queue>
#include <random>
#include <stack>
#include <iomanip>

using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
    return '"' + s + '"';
}

string to_string(const char* s) {
    return to_string((string) s);
}

string to_string(bool b) {
    return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
    bool first = true;
    string res = "{";
    for (int i = 0; i < static_cast<int>(v.size()); i++) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(v[i]);
    }
    res += "}";
    return res;
}

template <size_t N>
string to_string(bitset<N> v) {
    string res = "";
    for (size_t i = 0; i < N; i++) {
        res += static_cast<char>('0' + v[i]);
    }
    return res;
}

template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto &x : v) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

using Int = unsigned int;
using llong = long long;
using Llong = unsigned long long;
using ldouble = long double;
using intV = vector<int>;
using llongV = vector<long long>;
using llongVV = vector<vector<long long>>;
using Graph = vector<vector<long long>>;
using costGraph = vector<vector<pair<long long, long long>>>;

struct Edge {
    long long from;
    long long to;
    long long cost;
};

template<typename T>
using asc = less<T>();
template<typename T>
using desc = greater<T>();

const llong MOD = 1e9 + 7;
const llong INF = 1e17;

#define FOR(i, n) for (llong i = 0LL; i < llong(n); i++)
#define FORS(i, a, b) for (llong i = llong(a); i < llong(b); i++)
#define sup(vec, a) upper_bound(vec.begin(), vec.end(), a) - vec.begin()
#define inf(vec, a) lower_bound(vec.begin(), vec.end(), a) - vec.begin()

template<typename T>
class UnionFind {
    public :
        vector<T> parent;  // 親のインデックス
        vector<T> height;  // 木の高さ
        vector<T> size;  // 木の頂点数
        T component;  // 木の数

        UnionFind (T size_) : parent(size_), height(size_, 1), size(size_, 1) {
            for (T i = 0; i < size_; i++) {
                parent[i] = i;
            }
            component = size_;
        }
        void init (T size_) {
            parent.resize(size_);
            size.assign(size_, 1);
            component = size_;
            for (T i = 0; i < size_; i++) {
                parent[i] = i;
            }
        }

        // メソッド
        // Find
        T root(T index) {
            if (index == parent[index]) {
                return index;
            }
            parent[index] = root(parent[index]);
            return parent[index];
        }

        // Union
        bool merge(T index1, T index2) {
            T root1 = root(index1);
            T root2 = root(index2);

            if (root1 == root2) {
                return false;
            }

            component--;

            if (height[root1] < height[root2]) {
                parent[root1] = root2;
                size[root2] += size[root1];
            } else {
                parent[root2] = parent[root1];
                size[root1] += size[root2];
                if (height[root1] == height[root2]) {
                    height[root1]++;
                }
            }
            return true;
        }

        bool isSameRoot(T index1, T index2) {
            return root(index1) == root(index2);
        }

        T sizeOfSameRoot(T index) {
            return size[root(index)];
        }
};

vector<vector<int>> edges;

int search(int start, int N) {
    int INF = (int)1e9;
    vector<int> minDist(N, INF);
    queue<pair<int, int>> que;
    que.emplace(start, 0);
    while(!que.empty()) {
        int now, d;
        tie(now, d) = que.front();
        que.pop();
        if(minDist[now] <= d) continue;
        minDist[now] = d;
        for(int to : edges[now]) {
            if(minDist[to] == INF) {
                que.emplace(to, d + 1);
            };
        }
    }

    int ret = 0;
    for(int d : minDist) {
        if(d != INF) chmax(ret, d - 1);
    }
    return ret;
};

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

    int N, M;
    cin >> N >> M;
    UnionFind<int> tree(N);
    edges.resize(N);

    FOR(i, M) {
        int fr, to;
        cin >> fr >> to;
        fr--;
        to--;
        edges[fr].push_back(to);
        edges[to].push_back(fr);
        tree.merge(fr, to);
    }

    int Q;
    cin >> Q;
    vector<pair<int, int>> ans(Q);
    FOR(q, Q) {
        int start;
        cin >> start;
        start--;
        int dist = search(start, N);
        int day = 0;
        while((1 << day) <= dist) day++;
        ans[q] = make_pair(tree.sizeOfSameRoot(start) - 1, day);
    }

    for(auto a : ans) cout << a.first << " " << a.second << endl;

    return 0;
}
0