結果

問題 No.2290 UnUnion Find
ユーザー shobonvipshobonvip
提出日時 2023-05-05 21:25:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 408 ms / 2,000 ms
コード長 827 bytes
コンパイル時間 3,975 ms
コンパイル使用メモリ 268,216 KB
実行使用メモリ 13,568 KB
最終ジャッジ日時 2024-05-02 15:07:11
合計ジャッジ時間 23,308 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 307 ms
5,376 KB
testcase_03 AC 302 ms
8,448 KB
testcase_04 AC 314 ms
8,448 KB
testcase_05 AC 298 ms
8,320 KB
testcase_06 AC 317 ms
8,576 KB
testcase_07 AC 314 ms
8,448 KB
testcase_08 AC 303 ms
8,320 KB
testcase_09 AC 315 ms
8,448 KB
testcase_10 AC 303 ms
8,320 KB
testcase_11 AC 309 ms
8,344 KB
testcase_12 AC 307 ms
8,344 KB
testcase_13 AC 299 ms
8,448 KB
testcase_14 AC 312 ms
8,576 KB
testcase_15 AC 300 ms
8,448 KB
testcase_16 AC 306 ms
8,448 KB
testcase_17 AC 300 ms
8,320 KB
testcase_18 AC 303 ms
8,448 KB
testcase_19 AC 349 ms
9,216 KB
testcase_20 AC 397 ms
13,568 KB
testcase_21 AC 317 ms
5,376 KB
testcase_22 AC 315 ms
6,004 KB
testcase_23 AC 308 ms
5,888 KB
testcase_24 AC 365 ms
10,752 KB
testcase_25 AC 313 ms
5,376 KB
testcase_26 AC 390 ms
12,160 KB
testcase_27 AC 392 ms
11,392 KB
testcase_28 AC 326 ms
7,652 KB
testcase_29 AC 370 ms
10,112 KB
testcase_30 AC 326 ms
5,376 KB
testcase_31 AC 345 ms
9,600 KB
testcase_32 AC 317 ms
5,376 KB
testcase_33 AC 329 ms
7,440 KB
testcase_34 AC 408 ms
13,440 KB
testcase_35 AC 386 ms
11,904 KB
testcase_36 AC 316 ms
5,632 KB
testcase_37 AC 330 ms
6,944 KB
testcase_38 AC 319 ms
5,612 KB
testcase_39 AC 315 ms
6,144 KB
testcase_40 AC 377 ms
11,520 KB
testcase_41 AC 327 ms
5,376 KB
testcase_42 AC 347 ms
8,704 KB
testcase_43 AC 344 ms
8,576 KB
testcase_44 AC 304 ms
8,448 KB
testcase_45 AC 319 ms
8,320 KB
testcase_46 AC 322 ms
8,320 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