結果

問題 No.812 Change of Class
ユーザー tanimani364tanimani364
提出日時 2020-07-14 01:45:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,702 bytes
コンパイル時間 2,239 ms
コンパイル使用メモリ 206,436 KB
実行使用メモリ 14,224 KB
最終ジャッジ日時 2023-08-09 01:52:47
合計ジャッジ時間 11,095 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
12,448 KB
testcase_01 AC 9 ms
4,484 KB
testcase_02 AC 30 ms
8,100 KB
testcase_03 AC 64 ms
14,224 KB
testcase_04 AC 56 ms
12,640 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
4,612 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 13 ms
7,796 KB
testcase_12 WA -
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 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 AC 21 ms
4,476 KB
testcase_36 AC 8 ms
4,376 KB
testcase_37 AC 48 ms
5,432 KB
testcase_38 AC 6 ms
5,220 KB
testcase_39 AC 51 ms
5,508 KB
testcase_40 AC 12 ms
4,380 KB
testcase_41 AC 5 ms
5,004 KB
testcase_42 AC 112 ms
7,600 KB
testcase_43 AC 22 ms
6,724 KB
testcase_44 AC 75 ms
6,512 KB
testcase_45 AC 7 ms
4,376 KB
testcase_46 AC 20 ms
6,612 KB
testcase_47 AC 72 ms
6,536 KB
testcase_48 AC 83 ms
7,584 KB
testcase_49 AC 18 ms
4,376 KB
testcase_50 AC 3 ms
4,376 KB
testcase_51 AC 21 ms
4,704 KB
testcase_52 AC 45 ms
6,796 KB
testcase_53 AC 13 ms
4,380 KB
testcase_54 AC 10 ms
4,376 KB
testcase_55 AC 30 ms
7,952 KB
testcase_56 AC 34 ms
8,876 KB
testcase_57 AC 36 ms
9,304 KB
testcase_58 AC 20 ms
6,168 KB
testcase_59 AC 41 ms
9,916 KB
testcase_60 AC 2 ms
4,384 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

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

ll gcd(ll n, ll m)
{
	ll tmp;
	while (m != 0)
	{
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m)
{
	return abs(n) / gcd(n, m) * abs(m); //gl=xy
}

using namespace std;

 struct UF {
	vector<int>par;//親
	vector<int>Rank;//木の深さ
    vector<int>sz;//要素数

    UF(int n){init(n);}

	//初期化
	void init(int n) {
		par.resize(n); Rank.resize(n);sz.resize(n);
		for (int i = 0; i < n; i++) {
			par[i]= i;
			Rank[i] = 0;
            sz[i]=1;
		}
	}

	//木の根を求める
	int find(int x) {
		if (par[x] == x) {
			return x;
		}
		else {
			return par[x] = find(par[x]);
		}
	}

	//xとyの属する集合を併合
	bool unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y)return false;//すでに同じ集合に属している

		if (Rank[x] < Rank[y]) {//高さが小さい方を下に
			par[x] = y;
            sz[y]+=sz[x];
		}
		else {
			par[y] = x;
			if (Rank[x] == Rank[y])Rank[x]++;//同じだとrankが変わらないままになるので増やす(それ以外は大きい方のrankが採用される)
            sz[x]+=sz[y];
		}
		return true;
	}

	//xとyが同じ集合に属するかを判定
	bool same(int x, int y) {
		return find(x) == find(y);
	}

    int size(int k){//要素数を求める
        return sz[find(k)];
    }
};

vector<vector<int>>g;

void dfs(int v,int d,vector<int>& dis){
	dis[v]=d;
	for(auto x:g[v]){
		if(dis[x]==-1){
			dfs(x,d+1,dis);
		}
	}
}
void solve()
{
	int n,m;
	cin>>n>>m;
	g.resize(n);
	UF tree(n);
	rep(i,m){
		int p,q;
		cin>>p>>q;
		p--;q--;
		g[p].pb(q);
		g[q].pb(p);
		tree.unite(p,q);
	}
	int q;
	cin>>q;
	while(q--){
		int a;
		cin>>a;
		a--;
		int x=tree.size(tree.find(a))-1;
		cout<<x<<" ";
		vector<int>dis(n,-1);
		dfs(a,0,dis);
		int val=1;
		int cnt=0;
		int v=*max_element(all(dis));
		while(val<v){
			val*=2;
			++cnt;
		}
		cout<<cnt<<"\n";
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0