結果

問題 No.812 Change of Class
ユーザー kyort0n
提出日時 2019-04-12 21:35:45
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 259 ms / 4,000 ms
コード長 2,284 bytes
コンパイル時間 2,008 ms
コンパイル使用メモリ 177,068 KB
実行使用メモリ 11,164 KB
最終ジャッジ日時 2024-06-12 06:39:09
合計ジャッジ時間 8,431 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 60
権限があれば一括ダウンロードができます

ソースコード

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