結果

問題 No.556 仁義なきサルたち
ユーザー ふっぴーふっぴー
提出日時 2017-08-18 11:56:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 1,669 bytes
コンパイル時間 1,503 ms
コンパイル使用メモリ 166,180 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 15:43:48
合計ジャッジ時間 2,433 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
const int inf = 1000000001;
const ll INF = 1e16;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
#define Sp(p) cout<<setprecision(15)<<fixed<<p<<endl;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
#define N 10001
vi par(N); //親
vi ran(N); //木の深さ
vi num(N);

			   //n要素で初期化
void init(int n) {
	int i;
	for (i = 0; i < n; i++)
	{
		par[i] = i;
		ran[i] = 0;
		num[i] = 1;
	}
}

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

//xとyの属する集合を併合
void unite(int x, int y) {
	x = find(x);
	y = find(y);
	if (x == y) {
		return;
	}
	if (num[x] != num[y]) {
		if (num[x] > num[y]) {
			num[x] += num[y];
			par[y] = x;
		}
		else {
			num[y] += num[x];
			par[x] = y;
		}
	}
	else {
		if (par[y] > par[x]) {
			num[x] += num[y];
			par[y] = x;
		}
		else {
			num[y] += num[x];
			par[x] = y;
		}
	}
}

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

int main() {
	int n, m, i;
	cin >> n >> m;
	int a, b;
	init(n+1);
	for (i = 0; i < m; i++) {
		cin >> a >> b;
		unite(a, b);
	}
	for (i = 1; i < n+1; i++) {
		cout << find(i) << endl;
	}
	return 0;
}
0