結果

問題 No.556 仁義なきサルたち
ユーザー iwashi31iwashi31
提出日時 2017-08-11 23:23:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 2,013 bytes
コンパイル時間 988 ms
コンパイル使用メモリ 93,560 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 23:39:01
合計ジャッジ時間 1,859 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define itrep(i, a) for (auto i = (a).begin(); i != (a).end(); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, -1, 0, 1 };

template<class T> void inputVector(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < v.size(); i++) cin >> v[i];
}

class UnionFind {
private:
	vector<int> pastNode;

	class Node {
	public:
		int parent;
		int count;

		Node(int parent) {
			this->parent = parent;
			this->count = 1;
		}
	};

	vector<Node> node;
public:
	UnionFind(int size) {
		for (int i = 0; i < size; i++) {
			node.push_back(Node(i));
		}
	}

	int root(int x) {
		pastNode.clear();

		while (x != node[x].parent) {
			pastNode.push_back(x);
			x = node[x].parent;
		}
		rep(i, pastNode.size()) {
			node[pastNode[i]].parent = x;
		}
		return x;
	}

	void unite(int x, int y) {
		if (root(x) == root(y)) return;

		node[root(y)].count += node[root(x)].count;
		node[root(x)].parent = root(y);
	}

	bool same(int x, int y) {
		return root(x) == root(y);
	}

	int countSames(int x) {
		return node[root(x)].count;
	}
};

signed main() {
	int N, M;
	cin >> N >> M;

	UnionFind uf = UnionFind(N);

	rep(i, M) {
		int A, B;
		cin >> A >> B;
		A--; B--;

		if (uf.same(A, B)) continue;
		if (uf.countSames(A) < uf.countSames(B)) uf.unite(A, B);
		else if (uf.countSames(A) > uf.countSames(B)) uf.unite(B, A);
		else if (uf.root(A) < uf.root(B)) uf.unite(B, A);
		else uf.unite(A, B);
	}

	rep(i, N) {
		cout << uf.root(i) + 1 << endl;
	}

    return 0;
}
0