結果

問題 No.2290 UnUnion Find
ユーザー norikamenorikame
提出日時 2023-05-05 21:58:11
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 354 ms / 2,000 ms
コード長 1,044 bytes
コンパイル時間 5,928 ms
コンパイル使用メモリ 312,212 KB
実行使用メモリ 13,376 KB
最終ジャッジ日時 2023-08-15 04:00:47
合計ジャッジ時間 25,717 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 324 ms
4,380 KB
testcase_03 AC 283 ms
8,156 KB
testcase_04 AC 294 ms
8,272 KB
testcase_05 AC 293 ms
8,324 KB
testcase_06 AC 288 ms
8,228 KB
testcase_07 AC 284 ms
8,300 KB
testcase_08 AC 285 ms
8,240 KB
testcase_09 AC 290 ms
8,308 KB
testcase_10 AC 288 ms
8,256 KB
testcase_11 AC 287 ms
8,184 KB
testcase_12 AC 280 ms
8,240 KB
testcase_13 AC 284 ms
8,152 KB
testcase_14 AC 287 ms
8,112 KB
testcase_15 AC 287 ms
8,156 KB
testcase_16 AC 283 ms
8,156 KB
testcase_17 AC 287 ms
8,152 KB
testcase_18 AC 289 ms
8,208 KB
testcase_19 AC 318 ms
8,456 KB
testcase_20 AC 351 ms
13,376 KB
testcase_21 AC 332 ms
4,384 KB
testcase_22 AC 325 ms
5,780 KB
testcase_23 AC 327 ms
5,780 KB
testcase_24 AC 333 ms
9,704 KB
testcase_25 AC 331 ms
4,824 KB
testcase_26 AC 354 ms
13,156 KB
testcase_27 AC 336 ms
9,836 KB
testcase_28 AC 321 ms
6,536 KB
testcase_29 AC 334 ms
9,392 KB
testcase_30 AC 334 ms
4,380 KB
testcase_31 AC 327 ms
8,852 KB
testcase_32 AC 326 ms
4,948 KB
testcase_33 AC 317 ms
6,504 KB
testcase_34 AC 352 ms
13,220 KB
testcase_35 AC 341 ms
10,504 KB
testcase_36 AC 329 ms
5,680 KB
testcase_37 AC 321 ms
6,140 KB
testcase_38 AC 328 ms
4,872 KB
testcase_39 AC 321 ms
5,800 KB
testcase_40 AC 329 ms
10,320 KB
testcase_41 AC 328 ms
4,676 KB
testcase_42 AC 322 ms
8,220 KB
testcase_43 AC 322 ms
8,232 KB
testcase_44 AC 292 ms
8,152 KB
testcase_45 AC 309 ms
8,104 KB
testcase_46 AC 298 ms
8,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

using ll = long long;

#define rep(i, n) for (int i=0; i<(int)(n); ++(i))
#define rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i))
#define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i))
#define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i))
#define all(x) (x).begin(), (x).end()

const int INF = (int)(1e9);

int main() {
	int n, q;
	cin >> n >> q;
	dsu d(n);
	unordered_set<int> lst;
	rep(i, n) lst.insert(i);
	rep(i, q) {
		int qi;
		cin >> qi;
		if (qi == 1) {
			int ui, vi;
			cin >> ui >> vi;
			--ui; --vi;
			if (d.same(ui, vi)) continue;
			lst.erase(d.leader(ui));
			lst.erase(d.leader(vi));
			d.merge(ui, vi);
			lst.insert(d.leader(ui));
		}
		else {
			int vi;
			cin >> vi;
			--vi;
			if ((int)(lst.size()) == 1) {
				cout << -1 << endl;
				continue;
			}
			int ai = d.leader(vi);
			if (*lst.begin() == ai) ai = *next(lst.begin());
			else ai = *lst.begin();
			cout << (ai+1) << endl;
		}
	}
	return 0;
}
0