結果

問題 No.556 仁義なきサルたち
ユーザー treeonetreeone
提出日時 2017-08-12 01:05:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 919 bytes
コンパイル時間 1,446 ms
コンパイル使用メモリ 163,568 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 00:25:36
合計ジャッジ時間 2,391 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 8 ms
5,376 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 10 ms
5,376 KB
testcase_16 AC 16 ms
5,376 KB
testcase_17 AC 17 ms
5,376 KB
testcase_18 AC 20 ms
5,376 KB
testcase_19 AC 19 ms
5,376 KB
testcase_20 AC 20 ms
5,376 KB
testcase_21 AC 21 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
using namespace std;
typedef pair<int, int> P;

struct UF{
	vector<int> par;
	vector<int> sz;
	UF(){}
	UF(int n){
		par.resize(n);
		sz.resize(n, 1);
		rep(i, 0, n) par[i] = i;
	}
	int find(int x){
		if(x == par[x]) return x;
		return par[x] = find(par[x]);
	}
	void unite(int x, int y){
		x = find(x); y = find(y);
		if(x == y) return;
		if(sz[x] < sz[y]) swap(x, y);
		if(sz[x] == sz[y] && x > y) swap(x, y);
		sz[x] += sz[y];
		par[y] = x;
	}
	bool same(int x, int y){
		return find(x) == find(y);
	}
};

signed main(){
	int n, m;
	cin >> n >> m;
	UF uf(n);
	rep(i, 0, m){
		int a, b;
		cin >> a >> b;
		a--; b--;
		uf.unite(uf.find(a), uf.find(b));
	}
	rep(i, 0, n){
		cout << uf.find(i) + 1 << endl;
	}
}
0