結果

問題 No.556 仁義なきサルたち
ユーザー cielciel
提出日時 2017-08-12 01:56:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 759 bytes
コンパイル時間 604 ms
コンパイル使用メモリ 68,008 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 00:34:29
合計ジャッジ時間 1,423 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 5 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
class union_find{
	int *parent,*s,n;
public:
	int root(int a){return parent[a]==a?a:(parent[a]=root(parent[a]));}
	union_find(int _n){n=_n;parent=new int[n];s=new int[n];for(int i=0;i<n;i++)parent[i]=i,s[i]=1;}
	~union_find(){delete []parent;delete []s;}
	int same(int a,int b){return root(a)==root(b);}
	int unite(int a,int b){
		int x=root(a),y=root(b);if(x==y)return 0;
		if(s[x]>s[y] || (s[x]==s[y]&&x<y)){
			parent[y]=x;
			s[x]+=s[y];
		}else{
			parent[x]=y;
			s[y]+=s[x];
		}
		return 1;
	}
};
int main(){
	int n,m,a,b;
	scanf("%d%d",&n,&m);
	union_find uf(n);
	for(int i=0;i<m;i++)scanf("%d%d",&a,&b),uf.unite(a-1,b-1);
	for(int i=0;i<n;i++)printf("%d\n",uf.root(i)+1);
}
0