結果

問題 No.2290 UnUnion Find
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2023-05-05 21:52:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 441 ms / 2,000 ms
コード長 1,379 bytes
コンパイル時間 2,388 ms
コンパイル使用メモリ 210,800 KB
実行使用メモリ 13,928 KB
最終ジャッジ日時 2023-08-15 03:47:42
合計ジャッジ時間 24,094 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 315 ms
4,376 KB
testcase_03 AC 327 ms
8,520 KB
testcase_04 AC 333 ms
8,520 KB
testcase_05 AC 337 ms
8,568 KB
testcase_06 AC 336 ms
8,516 KB
testcase_07 AC 339 ms
8,568 KB
testcase_08 AC 331 ms
8,632 KB
testcase_09 AC 342 ms
8,572 KB
testcase_10 AC 337 ms
8,588 KB
testcase_11 AC 341 ms
8,572 KB
testcase_12 AC 344 ms
8,708 KB
testcase_13 AC 346 ms
8,492 KB
testcase_14 AC 339 ms
8,532 KB
testcase_15 AC 335 ms
8,588 KB
testcase_16 AC 338 ms
8,568 KB
testcase_17 AC 339 ms
8,524 KB
testcase_18 AC 340 ms
8,568 KB
testcase_19 AC 381 ms
9,316 KB
testcase_20 AC 441 ms
13,928 KB
testcase_21 AC 325 ms
4,376 KB
testcase_22 AC 345 ms
5,644 KB
testcase_23 AC 348 ms
5,692 KB
testcase_24 AC 403 ms
10,928 KB
testcase_25 AC 343 ms
5,016 KB
testcase_26 AC 423 ms
12,548 KB
testcase_27 AC 407 ms
11,704 KB
testcase_28 AC 354 ms
7,524 KB
testcase_29 AC 388 ms
10,368 KB
testcase_30 AC 332 ms
4,380 KB
testcase_31 AC 393 ms
9,612 KB
testcase_32 AC 344 ms
5,172 KB
testcase_33 AC 370 ms
7,468 KB
testcase_34 AC 435 ms
13,840 KB
testcase_35 AC 417 ms
12,520 KB
testcase_36 AC 342 ms
5,540 KB
testcase_37 AC 351 ms
7,048 KB
testcase_38 AC 338 ms
5,384 KB
testcase_39 AC 343 ms
5,944 KB
testcase_40 AC 412 ms
12,140 KB
testcase_41 AC 337 ms
4,904 KB
testcase_42 AC 371 ms
8,792 KB
testcase_43 AC 373 ms
8,636 KB
testcase_44 AC 337 ms
8,584 KB
testcase_45 AC 353 ms
8,564 KB
testcase_46 AC 357 ms
8,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = std::vector<int> ;
using Vl = vector<ll>;
using VVi = vector<Vi>;

class UnionFind {
	Vi par;
	Vi siz;
	Vl val;
	// VVi iv;
	
	public :
	set<int> st;
	UnionFind(int N = 0) {
		par.resize(N);
		siz.resize(N);
		// iv.resize(N);
		iota(par.begin(), par.end(), 0);
		fill(siz.begin(), siz.end(), 0);
		for (int i = 0; i < N; i ++) {
			// iv[i].push_back(i);
			st.insert(i);
		}
	}
	void init(int N) {
		par.resize(N);
		siz.resize(N);
		iota(par.begin(), par.end(), 0);
		fill(siz.begin(), siz.end(), 0);
	}
	int root(int v) {
		if (v == par[v]) {
			return v;
		} else {
			return par[v] = root(par[v]);
		}
	}
	bool unit(int a, int b) {
		a = root(a);
		b = root(b);
		if (a == b) {
			return false;
		}
		if (siz[a] < siz[b]) {
			swap(a, b);
		}
		par[b] = a;
		siz[a] += siz[b];
		/*for (auto x : iv[b]) {
			iv[a].push_back(x);
		}*/
		st.erase(b);
		return true;
	}
};
int main () {
	int N, Q;
	cin >> N >> Q;
	UnionFind uu(N);
	while (Q--) {
		int t;
		cin >> t;
		if (t - 1) {
			int a;
			cin >> a;
			if (uu.st.size() == 1) {
				puts("-1");
			} else {
				a = uu.root(--a);
				if (a != *uu.st.begin()) {
					cout << (*uu.st.begin()) + 1 << endl;
				} else {
					cout << (*uu.st.rbegin()) + 1 << endl;
				}
			}
		} else {
			int a, b;
			cin >> a >> b;
			uu.unit(--a, --b);
		}
	}
}
0