結果

問題 No.2290 UnUnion Find
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2023-05-05 21:52:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 426 ms / 2,000 ms
コード長 1,379 bytes
コンパイル時間 2,554 ms
コンパイル使用メモリ 211,780 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-05-02 16:00:48
合計ジャッジ時間 23,658 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 332 ms
5,376 KB
testcase_03 AC 328 ms
8,576 KB
testcase_04 AC 336 ms
8,832 KB
testcase_05 AC 336 ms
8,576 KB
testcase_06 AC 334 ms
8,704 KB
testcase_07 AC 341 ms
8,704 KB
testcase_08 AC 339 ms
8,704 KB
testcase_09 AC 333 ms
8,704 KB
testcase_10 AC 338 ms
8,832 KB
testcase_11 AC 338 ms
8,704 KB
testcase_12 AC 344 ms
8,704 KB
testcase_13 AC 340 ms
8,704 KB
testcase_14 AC 338 ms
8,704 KB
testcase_15 AC 334 ms
8,704 KB
testcase_16 AC 333 ms
8,704 KB
testcase_17 AC 336 ms
8,704 KB
testcase_18 AC 338 ms
8,704 KB
testcase_19 AC 378 ms
9,472 KB
testcase_20 AC 426 ms
14,208 KB
testcase_21 AC 339 ms
5,376 KB
testcase_22 AC 351 ms
5,888 KB
testcase_23 AC 348 ms
5,888 KB
testcase_24 AC 390 ms
11,264 KB
testcase_25 AC 352 ms
5,376 KB
testcase_26 AC 404 ms
12,800 KB
testcase_27 AC 402 ms
11,776 KB
testcase_28 AC 351 ms
7,680 KB
testcase_29 AC 404 ms
10,880 KB
testcase_30 AC 339 ms
5,376 KB
testcase_31 AC 378 ms
9,984 KB
testcase_32 AC 343 ms
5,376 KB
testcase_33 AC 359 ms
7,680 KB
testcase_34 AC 421 ms
14,208 KB
testcase_35 AC 411 ms
12,544 KB
testcase_36 AC 351 ms
5,504 KB
testcase_37 AC 357 ms
7,040 KB
testcase_38 AC 343 ms
5,504 KB
testcase_39 AC 360 ms
6,144 KB
testcase_40 AC 406 ms
12,032 KB
testcase_41 AC 353 ms
5,376 KB
testcase_42 AC 373 ms
9,088 KB
testcase_43 AC 384 ms
8,832 KB
testcase_44 AC 337 ms
8,704 KB
testcase_45 AC 352 ms
8,704 KB
testcase_46 AC 354 ms
8,704 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