結果

問題 No.556 仁義なきサルたち
ユーザー 👑 jupirojupiro
提出日時 2020-02-08 04:49:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,876 bytes
コンパイル時間 1,416 ms
コンパイル使用メモリ 128,292 KB
実行使用メモリ 4,560 KB
最終ジャッジ日時 2023-10-26 00:41:12
合計ジャッジ時間 2,354 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
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 AC 9 ms
4,516 KB
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

class UnionFind {
public:
	vector<ll> Parent;
	UnionFind(ll N) {
		Parent = vector<ll>(N, -1);
	}
	ll root(ll A) {
		if (Parent[A] < 0) {
			return A;
		}
		return Parent[A] = root(Parent[A]);
	}

	ll size(ll A) {
		return -Parent[root(A)];
	}

	bool connect(ll A, ll B) {
		A = root(A);
		B = root(B);
		if (A == B) {
			return false;
		}
		if (size(A) < size(B)) {
			swap(A, B);
		}

		Parent[A] += Parent[B];
		Parent[B] = A;

		return true;
	}

};

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	ll n, m; scanf("%lld %lld", &n, &m);
	UnionFind uni(n + 1);
	map<ll, ll> mp;
	for (ll i = 1; i <= n; i++) mp[i] = i;
	for (ll i = 0; i < m; i++) {
		ll a, b; scanf("%lld %lld", &a, &b);
		if (uni.root(a) == uni.root(b)) continue;
		if (uni.size(a) < uni.size(b)) swap(a, b);
		mp.erase(mp.find(uni.root(b)));
		ll boss = mp[uni.root(a)];
		mp.erase(mp.find(uni.root(a)));
		uni.connect(a, b);
		mp[uni.root(a)] = boss;
	}

	for (ll i = 1; i <= n; i++) {
		printf("%lld\n", mp[uni.root(i)]);
	}
	return 0;
}
0