結果

問題 No.556 仁義なきサルたち
ユーザー square1001square1001
提出日時 2017-08-11 22:37:47
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 609 bytes
コンパイル時間 562 ms
コンパイル使用メモリ 67,596 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-20 22:52:41
合計ジャッジ時間 2,672 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 5 ms
6,940 KB
testcase_11 AC 5 ms
6,940 KB
testcase_12 AC 6 ms
6,940 KB
testcase_13 AC 11 ms
6,944 KB
testcase_14 AC 41 ms
6,940 KB
testcase_15 AC 62 ms
6,940 KB
testcase_16 AC 79 ms
6,944 KB
testcase_17 AC 150 ms
6,944 KB
testcase_18 AC 283 ms
6,940 KB
testcase_19 AC 279 ms
6,940 KB
testcase_20 AC 283 ms
6,940 KB
testcase_21 AC 283 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;
int N, M, a, b, par[10009];
int sz(int x) {
	int ret = 0;
	for (int i = 0; i < N; i++) {
		if (par[i] == par[x]) ret++;
	}
	return ret;
}
int main() {
	cin >> N >> M;
	for (int i = 0; i < N; i++) par[i] = i;
	for (int i = 0; i < M; i++) {
		cin >> a >> b; a--, b--;
		int sa = sz(a), sb = sz(b);
		if (sa < sb) swap(a, b);
		else if (sa == sb && par[a] > par[b]) swap(a, b);
		int pb = par[b];
		for (int j = 0; j < N; j++) {
			if (par[j] == pb) par[j] = par[a];
		}
	}
	for (int i = 0; i < N; i++) {
		cout << par[i] + 1 << endl;
	}
	return 0;
}
0