結果

問題 No.2290 UnUnion Find
ユーザー ripityripity
提出日時 2023-05-05 21:55:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 399 ms / 2,000 ms
コード長 1,019 bytes
コンパイル時間 6,690 ms
コンパイル使用メモリ 302,840 KB
実行使用メモリ 24,612 KB
最終ジャッジ日時 2023-08-15 03:56:37
合計ジャッジ時間 27,272 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 282 ms
4,384 KB
testcase_03 AC 262 ms
14,196 KB
testcase_04 AC 263 ms
14,240 KB
testcase_05 AC 279 ms
14,332 KB
testcase_06 AC 274 ms
14,292 KB
testcase_07 AC 280 ms
14,208 KB
testcase_08 AC 262 ms
14,212 KB
testcase_09 AC 271 ms
14,212 KB
testcase_10 AC 277 ms
14,340 KB
testcase_11 AC 283 ms
14,340 KB
testcase_12 AC 271 ms
14,356 KB
testcase_13 AC 277 ms
14,304 KB
testcase_14 AC 277 ms
14,248 KB
testcase_15 AC 281 ms
14,264 KB
testcase_16 AC 261 ms
14,232 KB
testcase_17 AC 262 ms
14,292 KB
testcase_18 AC 260 ms
14,328 KB
testcase_19 AC 324 ms
16,176 KB
testcase_20 AC 399 ms
24,556 KB
testcase_21 AC 277 ms
4,420 KB
testcase_22 AC 286 ms
8,844 KB
testcase_23 AC 283 ms
8,844 KB
testcase_24 AC 351 ms
19,516 KB
testcase_25 AC 285 ms
7,328 KB
testcase_26 AC 364 ms
22,320 KB
testcase_27 AC 370 ms
20,496 KB
testcase_28 AC 307 ms
12,800 KB
testcase_29 AC 345 ms
18,488 KB
testcase_30 AC 284 ms
5,200 KB
testcase_31 AC 326 ms
17,204 KB
testcase_32 AC 280 ms
7,760 KB
testcase_33 AC 297 ms
12,624 KB
testcase_34 AC 396 ms
24,612 KB
testcase_35 AC 366 ms
21,936 KB
testcase_36 AC 284 ms
8,156 KB
testcase_37 AC 286 ms
11,216 KB
testcase_38 AC 282 ms
7,996 KB
testcase_39 AC 285 ms
9,212 KB
testcase_40 AC 386 ms
21,024 KB
testcase_41 AC 288 ms
7,144 KB
testcase_42 AC 337 ms
15,520 KB
testcase_43 AC 354 ms
14,824 KB
testcase_44 AC 264 ms
14,476 KB
testcase_45 AC 289 ms
14,296 KB
testcase_46 AC 287 ms
14,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, Q;
	cin >> N >> Q;
	vector<int> leader(N);
	vector<vector<int>> group(N, vector<int>());
	set<int> ls;
	for( int i = 0; i < N; i++ ) {
		leader[i] = i;
		group[i].push_back(i);
		ls.insert(i);
	}
	while(Q--) {
		int t;
		cin >> t;
		if( t == 1 ) {
			int u, v;
			cin >> u >> v;
			u--, v--;
			if( leader[u] != leader[v] ) {
				u = leader[u], v = leader[v];
				if( group[u].size() > group[v].size() ) swap(u, v);
				ls.erase(u);
				for( int& x : group[u] ) {
					leader[x] = v;
					group[v].push_back(x);
				}
			}
		}else {
			int u;
			cin >> u;
			u--;
			if( ls.size() == 1 ) {
				cout << -1 << endl;
			}else {
				if( leader[u] == *ls.begin() ) {
					cout << *next(ls.begin())+1 << endl;
				}else {
					cout << *ls.begin()+1 << endl;
				}
			}
		}
	}
}
0