結果

問題 No.2290 UnUnion Find
ユーザー norikamenorikame
提出日時 2023-05-05 21:58:11
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 318 ms / 2,000 ms
コード長 1,044 bytes
コンパイル時間 4,673 ms
コンパイル使用メモリ 313,528 KB
実行使用メモリ 13,452 KB
最終ジャッジ日時 2024-05-02 16:14:14
合計ジャッジ時間 22,084 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 292 ms
5,376 KB
testcase_03 AC 263 ms
8,340 KB
testcase_04 AC 249 ms
8,340 KB
testcase_05 AC 247 ms
8,336 KB
testcase_06 AC 255 ms
8,336 KB
testcase_07 AC 254 ms
8,336 KB
testcase_08 AC 246 ms
8,336 KB
testcase_09 AC 255 ms
8,340 KB
testcase_10 AC 259 ms
8,336 KB
testcase_11 AC 250 ms
8,464 KB
testcase_12 AC 250 ms
8,468 KB
testcase_13 AC 259 ms
8,464 KB
testcase_14 AC 246 ms
8,340 KB
testcase_15 AC 257 ms
8,336 KB
testcase_16 AC 249 ms
8,340 KB
testcase_17 AC 255 ms
8,592 KB
testcase_18 AC 255 ms
8,336 KB
testcase_19 AC 281 ms
8,636 KB
testcase_20 AC 296 ms
13,452 KB
testcase_21 AC 314 ms
5,376 KB
testcase_22 AC 298 ms
5,864 KB
testcase_23 AC 288 ms
5,828 KB
testcase_24 AC 291 ms
9,788 KB
testcase_25 AC 295 ms
5,376 KB
testcase_26 AC 284 ms
13,352 KB
testcase_27 AC 288 ms
10,088 KB
testcase_28 AC 276 ms
6,864 KB
testcase_29 AC 280 ms
9,496 KB
testcase_30 AC 318 ms
5,376 KB
testcase_31 AC 281 ms
8,924 KB
testcase_32 AC 298 ms
5,376 KB
testcase_33 AC 279 ms
6,852 KB
testcase_34 AC 303 ms
13,452 KB
testcase_35 AC 289 ms
10,648 KB
testcase_36 AC 301 ms
5,760 KB
testcase_37 AC 287 ms
6,428 KB
testcase_38 AC 306 ms
5,376 KB
testcase_39 AC 298 ms
5,876 KB
testcase_40 AC 292 ms
10,228 KB
testcase_41 AC 295 ms
5,376 KB
testcase_42 AC 276 ms
8,368 KB
testcase_43 AC 279 ms
8,476 KB
testcase_44 AC 259 ms
8,464 KB
testcase_45 AC 270 ms
8,340 KB
testcase_46 AC 279 ms
8,336 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