結果

問題 No.812 Change of Class
ユーザー sinsincoscossinsincoscos
提出日時 2019-04-12 22:27:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 4,000 ms
コード長 1,563 bytes
コンパイル時間 995 ms
コンパイル使用メモリ 85,144 KB
実行使用メモリ 10,156 KB
最終ジャッジ日時 2023-09-03 13:21:08
合計ジャッジ時間 7,047 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
8,832 KB
testcase_01 AC 14 ms
5,972 KB
testcase_02 AC 40 ms
7,212 KB
testcase_03 AC 85 ms
9,472 KB
testcase_04 AC 75 ms
8,896 KB
testcase_05 AC 176 ms
9,200 KB
testcase_06 AC 152 ms
8,696 KB
testcase_07 AC 3 ms
6,148 KB
testcase_08 AC 3 ms
5,192 KB
testcase_09 AC 165 ms
9,360 KB
testcase_10 AC 169 ms
9,660 KB
testcase_11 AC 17 ms
7,364 KB
testcase_12 AC 109 ms
9,096 KB
testcase_13 AC 3 ms
5,300 KB
testcase_14 AC 3 ms
5,400 KB
testcase_15 AC 111 ms
8,000 KB
testcase_16 AC 9 ms
5,560 KB
testcase_17 AC 106 ms
8,364 KB
testcase_18 AC 79 ms
7,632 KB
testcase_19 AC 91 ms
7,888 KB
testcase_20 AC 168 ms
9,412 KB
testcase_21 AC 76 ms
7,396 KB
testcase_22 AC 35 ms
6,212 KB
testcase_23 AC 8 ms
5,652 KB
testcase_24 AC 12 ms
5,924 KB
testcase_25 AC 28 ms
6,232 KB
testcase_26 AC 137 ms
8,668 KB
testcase_27 AC 124 ms
8,392 KB
testcase_28 AC 86 ms
7,720 KB
testcase_29 AC 22 ms
5,896 KB
testcase_30 AC 110 ms
8,104 KB
testcase_31 AC 92 ms
7,828 KB
testcase_32 AC 45 ms
6,564 KB
testcase_33 AC 118 ms
8,620 KB
testcase_34 AC 11 ms
5,720 KB
testcase_35 AC 23 ms
6,304 KB
testcase_36 AC 11 ms
5,720 KB
testcase_37 AC 54 ms
6,760 KB
testcase_38 AC 7 ms
6,244 KB
testcase_39 AC 54 ms
6,984 KB
testcase_40 AC 15 ms
5,932 KB
testcase_41 AC 6 ms
5,912 KB
testcase_42 AC 112 ms
8,124 KB
testcase_43 AC 31 ms
7,252 KB
testcase_44 AC 79 ms
7,380 KB
testcase_45 AC 10 ms
5,616 KB
testcase_46 AC 31 ms
7,248 KB
testcase_47 AC 76 ms
7,292 KB
testcase_48 AC 86 ms
8,056 KB
testcase_49 AC 24 ms
6,300 KB
testcase_50 AC 5 ms
5,348 KB
testcase_51 AC 26 ms
6,304 KB
testcase_52 AC 51 ms
7,468 KB
testcase_53 AC 16 ms
5,980 KB
testcase_54 AC 14 ms
5,664 KB
testcase_55 AC 48 ms
8,908 KB
testcase_56 AC 58 ms
9,200 KB
testcase_57 AC 60 ms
9,508 KB
testcase_58 AC 32 ms
7,628 KB
testcase_59 AC 67 ms
10,156 KB
testcase_60 AC 4 ms
5,452 KB
testcase_61 AC 3 ms
5,268 KB
testcase_62 AC 3 ms
5,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <queue>
using namespace std;

class UnionFind{
private:
    vector<int> p,s;
public:
	UnionFind(){}
	UnionFind(int N){
		p = s = vector<int>(N+1,0);
		for(int i=1;i<=N;i++){
			p[i] = i; s[i] = 1;
		}
	}
	int find(int x){
		if(p[x]==x) return x;
		else return p[x] = find(p[x]);
	}
	void unite(int x,int y){
		x = find(x); y = find(y);
		if(x==y) return;
		if(s[x]>s[y]){
			p[y] = x;
			s[x] += s[y];
		}else{
			p[x] = y;
			s[y] += s[x];
		}
	}
	bool is_same_set(int x,int y) {return find(x)==find(y);}
	int size(int x) {return s[find(x)];}
};


int N,M,p,q,Q,a;
int dist[100010] = {};
vector<vector<int>> v(100010);

int bfs(int n){
    for(int i=1;i<=N;i++) dist[i] = -1;
    queue<int> Q;
    dist[n] = 0;
    Q.push(n);
    int res = 0;
    while(!Q.empty()){
        int a = Q.front(); Q.pop();
        for(auto x:v[a]){
            if(dist[x]==-1){
                dist[x] = dist[a]+1;
                Q.push(x);
                res = max(res,dist[x]);
            }
        }
    }
    return res;
}

int main(){
    cin >> N >> M;
    UnionFind uf(N);
    for(int i=0;i<M;i++){
        cin >> p >> q;
        v[p].push_back(q);
        v[q].push_back(p);
        uf.unite(p,q);
    }
    cin >> Q;
    for(int i=0;i<Q;i++){
        cin >> a;
        cout << uf.size(a)-1 << " ";
        int d = bfs(a);
        int ans = 0;
        for(int i=0;i<20;i++){
            if(d<=(1<<i)){
                ans = i;
                break;
            }
        }
        cout << ans << endl;
    }
}
0