結果

問題 No.812 Change of Class
ユーザー aa
提出日時 2019-04-13 12:25:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 4,000 ms
コード長 1,369 bytes
コンパイル時間 1,879 ms
コンパイル使用メモリ 174,556 KB
実行使用メモリ 10,400 KB
最終ジャッジ日時 2023-09-03 14:48:33
合計ジャッジ時間 8,413 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
8,852 KB
testcase_01 AC 12 ms
4,476 KB
testcase_02 AC 42 ms
6,592 KB
testcase_03 AC 88 ms
9,736 KB
testcase_04 AC 78 ms
8,904 KB
testcase_05 AC 185 ms
9,032 KB
testcase_06 AC 158 ms
7,660 KB
testcase_07 AC 3 ms
4,992 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 175 ms
9,772 KB
testcase_10 AC 179 ms
9,796 KB
testcase_11 AC 16 ms
7,668 KB
testcase_12 AC 117 ms
9,572 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 118 ms
7,696 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 112 ms
8,060 KB
testcase_18 AC 84 ms
6,596 KB
testcase_19 AC 95 ms
7,180 KB
testcase_20 AC 177 ms
9,528 KB
testcase_21 AC 78 ms
6,488 KB
testcase_22 AC 36 ms
5,108 KB
testcase_23 AC 7 ms
4,380 KB
testcase_24 AC 11 ms
4,376 KB
testcase_25 AC 29 ms
4,916 KB
testcase_26 AC 149 ms
8,636 KB
testcase_27 AC 128 ms
8,024 KB
testcase_28 AC 91 ms
7,024 KB
testcase_29 AC 21 ms
4,608 KB
testcase_30 AC 115 ms
7,848 KB
testcase_31 AC 98 ms
6,884 KB
testcase_32 AC 45 ms
5,508 KB
testcase_33 AC 123 ms
8,408 KB
testcase_34 AC 10 ms
4,376 KB
testcase_35 AC 23 ms
4,952 KB
testcase_36 AC 9 ms
4,380 KB
testcase_37 AC 54 ms
5,648 KB
testcase_38 AC 6 ms
5,196 KB
testcase_39 AC 56 ms
5,756 KB
testcase_40 AC 15 ms
4,420 KB
testcase_41 AC 5 ms
5,060 KB
testcase_42 AC 117 ms
7,684 KB
testcase_43 AC 33 ms
6,640 KB
testcase_44 AC 81 ms
6,484 KB
testcase_45 AC 10 ms
4,380 KB
testcase_46 AC 30 ms
6,612 KB
testcase_47 AC 80 ms
6,600 KB
testcase_48 AC 89 ms
7,208 KB
testcase_49 AC 24 ms
4,732 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 26 ms
5,332 KB
testcase_52 AC 55 ms
6,796 KB
testcase_53 AC 16 ms
4,380 KB
testcase_54 AC 13 ms
4,380 KB
testcase_55 AC 57 ms
8,196 KB
testcase_56 AC 68 ms
9,368 KB
testcase_57 AC 72 ms
9,560 KB
testcase_58 AC 34 ms
6,472 KB
testcase_59 AC 81 ms
10,400 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

struct unionfind
{
	//rankのオーバーフローに注意
	vector<int> par, rank;
	unionfind(int n)
	{
		par.resize(n);
		rank.resize(n);
		for (int i = 0; i < n; i++)
		{
			par[i] = i;
			rank[i] = 1;
		}
	}

	bool same(int u, int v)
	{
		return root(u) == root(v);
	}

	void unite(int u, int v)
	{
		if (same(u, v))
			return;
		u = root(u), v = root(v);
		if (rank[u] > rank[v])
			swap(u, v);
		par[u] = v;
		rank[v] += rank[u];
	}

	int root(int v)
	{
		if (par[v] == v)
			return v;
		return par[v] = root(par[v]);
	}
};

void solve()
{
	int n, m, q;
	cin >> n >> m;
	unionfind uf(n);
	vector<int> g[n];
	while (m--)
	{
		int u, v;
		cin >> u >> v;
		u--, v--;
		uf.unite(u, v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	cin >> q;
	while(q--)
	{
		int a, s;
		cin >> a;
		a--;
		s = uf.rank[uf.root(a)] - 1;
		cout << s << ' ';
		vector<int> d(n, -1);
		d[a] = 0;
		queue<int> q;
		q.push(a);
		int maxi = 0;
		while (!q.empty())
		{
			int v = q.front();
			q.pop();
			for (int nv : g[v])
			{
				if (d[nv] != -1) continue;
				d[nv] = d[v] + 1;
				q.push(nv);
				maxi = max(maxi, d[nv]);
			}
		}
		int l = -1, r = 20;
		while(l+1<r)
		{
			int m = (l + r) / 2;
			(pow(2, m) >= maxi ? r : l) = m;
		}
		cout << r << endl;
	}
}

int main()
{
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}
0