結果

問題 No.556 仁義なきサルたち
ユーザー olpheolphe
提出日時 2017-08-11 22:34:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,355 bytes
コンパイル時間 941 ms
コンパイル使用メモリ 107,800 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-20 22:49:35
合計ジャッジ時間 1,757 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;
const long double EPS = 0.0000000001;

long long int N, M, K, Q, W, H, L, R;
long long int ans;

class UnionFind {
	vector<int>parent;
	vector<int>rank;
public:
	UnionFind(int num) {
		num++;
		parent.resize(num);
		rank.resize(num);
		for (int i = 0; i < num; i++) {
			parent[i] = i;
			rank[i] = 1;
		}
	}
	int Find(int node) {
		if (parent[node] == node)return node;
		else return parent[node] = Find(parent[node]);
	}
	void Unite(int u, int v) {
		u = Find(u);
		v = Find(v);
		if (u == v)return;
		if (rank[u] < rank[v]) {
			parent[v] = u;
			rank[v] += rank[u];
		}
		else if (rank[u] > rank[v]) {
			parent[u] = v;
			rank[u] += rank[v];
		}
		else {
			if (u < v) {
				parent[v] = u;
				rank[u] += rank[v];
			}
			else {
				parent[u] = v;
				rank[v] += rank[u];
			}
		}
	}
	bool Check_Same(int u, int v) {
		return Find(u) == Find(v);
	}
};

int main() {
	ios::sync_with_stdio(false);
	UnionFind uf(20000);
	cin >> N >> M;
	for (int i = 0; i < M; i++) {
		cin >> L >> R;
		uf.Unite(L, R);
	}
	for (int i = 1; i <= N; i++)cout << uf.Find(i) << endl;
	return 0;
}
0