結果

問題 No.2290 UnUnion Find
ユーザー shobonvipshobonvip
提出日時 2023-05-05 21:25:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 512 ms / 2,000 ms
コード長 827 bytes
コンパイル時間 4,768 ms
コンパイル使用メモリ 266,576 KB
実行使用メモリ 13,124 KB
最終ジャッジ日時 2023-08-15 02:49:12
合計ジャッジ時間 27,066 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,388 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 316 ms
4,380 KB
testcase_03 AC 345 ms
8,192 KB
testcase_04 AC 341 ms
8,140 KB
testcase_05 AC 347 ms
8,104 KB
testcase_06 AC 341 ms
8,232 KB
testcase_07 AC 346 ms
8,108 KB
testcase_08 AC 346 ms
8,088 KB
testcase_09 AC 344 ms
8,084 KB
testcase_10 AC 347 ms
8,364 KB
testcase_11 AC 343 ms
8,360 KB
testcase_12 AC 345 ms
8,080 KB
testcase_13 AC 345 ms
8,228 KB
testcase_14 AC 347 ms
8,132 KB
testcase_15 AC 347 ms
8,164 KB
testcase_16 AC 348 ms
8,160 KB
testcase_17 AC 347 ms
8,104 KB
testcase_18 AC 347 ms
8,076 KB
testcase_19 AC 426 ms
8,968 KB
testcase_20 AC 509 ms
13,108 KB
testcase_21 AC 335 ms
4,380 KB
testcase_22 AC 354 ms
5,800 KB
testcase_23 AC 349 ms
5,788 KB
testcase_24 AC 462 ms
10,424 KB
testcase_25 AC 347 ms
5,276 KB
testcase_26 AC 486 ms
12,108 KB
testcase_27 AC 475 ms
11,000 KB
testcase_28 AC 383 ms
7,592 KB
testcase_29 AC 447 ms
9,960 KB
testcase_30 AC 338 ms
4,380 KB
testcase_31 AC 432 ms
9,548 KB
testcase_32 AC 346 ms
5,220 KB
testcase_33 AC 381 ms
7,188 KB
testcase_34 AC 512 ms
13,124 KB
testcase_35 AC 485 ms
12,084 KB
testcase_36 AC 344 ms
5,384 KB
testcase_37 AC 371 ms
6,804 KB
testcase_38 AC 351 ms
5,356 KB
testcase_39 AC 361 ms
5,984 KB
testcase_40 AC 481 ms
11,224 KB
testcase_41 AC 347 ms
4,976 KB
testcase_42 AC 421 ms
8,704 KB
testcase_43 AC 417 ms
8,424 KB
testcase_44 AC 350 ms
8,160 KB
testcase_45 AC 360 ms
8,096 KB
testcase_46 AC 361 ms
8,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef modint998244353 mint;
typedef long long ll;

int main(){
	int n, q; cin >> n >> q;
	dsu uf(n);
	set<int> alive;
	for (int i=0; i<n; i++){
		alive.insert(i);
	}
	for (int i=0; i<q; i++){
		int t; cin >> t;
		if (t == 1){
			int u, v; cin >> u >> v;
			u--; v--;
			if (uf.same(u, v)) continue;
			int x = uf.leader(u);
			int y = uf.leader(v);
			alive.erase(x);
			alive.erase(y);
			uf.merge(u, v);
			int z = uf.leader(u);
			alive.insert(z);
		}else{
			int u; cin >> u;
			u--;
			auto itr = alive.begin();
			int k = *itr;
			if (k != uf.leader(u)){
				cout << k+1 << "\n";
				continue;
			}
			itr++;
			if (itr == alive.end()){
				cout << -1 << "\n";
				continue;
			}
			k = *itr;
			cout << k+1 << "\n";

		}
	}
}
0