結果

問題 No.2290 UnUnion Find
ユーザー achapiachapi
提出日時 2023-05-05 22:19:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 539 ms / 2,000 ms
コード長 678 bytes
コンパイル時間 5,776 ms
コンパイル使用メモリ 267,000 KB
実行使用メモリ 13,108 KB
最終ジャッジ日時 2023-08-15 04:44:35
合計ジャッジ時間 27,767 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 337 ms
4,384 KB
testcase_03 AC 340 ms
8,068 KB
testcase_04 AC 329 ms
8,136 KB
testcase_05 AC 343 ms
8,272 KB
testcase_06 AC 350 ms
8,084 KB
testcase_07 AC 337 ms
8,076 KB
testcase_08 AC 340 ms
8,172 KB
testcase_09 AC 340 ms
8,092 KB
testcase_10 AC 334 ms
8,212 KB
testcase_11 AC 339 ms
8,208 KB
testcase_12 AC 336 ms
8,128 KB
testcase_13 AC 338 ms
8,176 KB
testcase_14 AC 336 ms
8,176 KB
testcase_15 AC 335 ms
8,072 KB
testcase_16 AC 337 ms
8,088 KB
testcase_17 AC 336 ms
8,208 KB
testcase_18 AC 338 ms
8,076 KB
testcase_19 AC 414 ms
8,836 KB
testcase_20 AC 501 ms
13,108 KB
testcase_21 AC 344 ms
4,384 KB
testcase_22 AC 358 ms
5,716 KB
testcase_23 AC 357 ms
5,716 KB
testcase_24 AC 463 ms
10,468 KB
testcase_25 AC 350 ms
5,132 KB
testcase_26 AC 488 ms
12,112 KB
testcase_27 AC 462 ms
11,064 KB
testcase_28 AC 387 ms
7,248 KB
testcase_29 AC 438 ms
9,944 KB
testcase_30 AC 353 ms
4,384 KB
testcase_31 AC 430 ms
9,680 KB
testcase_32 AC 349 ms
5,192 KB
testcase_33 AC 372 ms
7,172 KB
testcase_34 AC 512 ms
13,108 KB
testcase_35 AC 489 ms
11,904 KB
testcase_36 AC 361 ms
5,368 KB
testcase_37 AC 377 ms
6,652 KB
testcase_38 AC 353 ms
5,452 KB
testcase_39 AC 355 ms
5,964 KB
testcase_40 AC 539 ms
11,212 KB
testcase_41 AC 347 ms
5,056 KB
testcase_42 AC 423 ms
8,912 KB
testcase_43 AC 410 ms
8,304 KB
testcase_44 AC 355 ms
8,172 KB
testcase_45 AC 365 ms
8,140 KB
testcase_46 AC 364 ms
8,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
	int n, q;
	cin >> n >> q;
	atcoder::dsu uf(n);
	multiset<int> s;
	for (int i = 0; i < n; i++){
		s.insert(i);
	}
	while (q--){
		int t;
		cin >> t;
		if (t == 1){
			int u, v;
			cin >> u >> v;
			u--;
			v--;
			s.erase(uf.leader(u));
			s.erase(uf.leader(v));
			uf.merge(u, v);
			s.insert(uf.leader(u));
			s.insert(uf.leader(v));
		} else {
			int u;
			cin >> u;
			u--;
			if ((*s.begin()) != uf.leader(u)){
				cout << (*s.begin()) + 1 << endl;
			} else if ((*prev(s.end())) != uf.leader(u)) {
				cout << (*prev(s.end())) + 1 << endl;
			} else {
				cout << -1 << endl;
			}
		}
	}
}
0