結果

問題 No.2290 UnUnion Find
ユーザー ripityripity
提出日時 2023-05-05 21:55:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 327 ms / 2,000 ms
コード長 1,019 bytes
コンパイル時間 5,761 ms
コンパイル使用メモリ 305,076 KB
実行使用メモリ 24,832 KB
最終ジャッジ日時 2024-05-02 16:09:54
合計ジャッジ時間 21,839 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 244 ms
5,376 KB
testcase_03 AC 221 ms
14,336 KB
testcase_04 AC 226 ms
14,464 KB
testcase_05 AC 220 ms
14,464 KB
testcase_06 AC 216 ms
14,360 KB
testcase_07 AC 219 ms
14,464 KB
testcase_08 AC 224 ms
14,336 KB
testcase_09 AC 224 ms
14,464 KB
testcase_10 AC 220 ms
14,496 KB
testcase_11 AC 226 ms
14,464 KB
testcase_12 AC 230 ms
14,464 KB
testcase_13 AC 215 ms
14,464 KB
testcase_14 AC 217 ms
14,464 KB
testcase_15 AC 218 ms
14,364 KB
testcase_16 AC 216 ms
14,496 KB
testcase_17 AC 221 ms
14,368 KB
testcase_18 AC 222 ms
14,464 KB
testcase_19 AC 258 ms
16,256 KB
testcase_20 AC 324 ms
24,832 KB
testcase_21 AC 245 ms
5,376 KB
testcase_22 AC 252 ms
8,960 KB
testcase_23 AC 255 ms
9,088 KB
testcase_24 AC 279 ms
19,712 KB
testcase_25 AC 252 ms
7,424 KB
testcase_26 AC 312 ms
22,400 KB
testcase_27 AC 297 ms
20,480 KB
testcase_28 AC 245 ms
12,800 KB
testcase_29 AC 289 ms
18,816 KB
testcase_30 AC 248 ms
5,376 KB
testcase_31 AC 278 ms
17,280 KB
testcase_32 AC 247 ms
7,808 KB
testcase_33 AC 241 ms
12,672 KB
testcase_34 AC 327 ms
24,832 KB
testcase_35 AC 289 ms
22,016 KB
testcase_36 AC 248 ms
8,192 KB
testcase_37 AC 250 ms
11,392 KB
testcase_38 AC 247 ms
8,064 KB
testcase_39 AC 241 ms
9,344 KB
testcase_40 AC 290 ms
21,120 KB
testcase_41 AC 244 ms
7,296 KB
testcase_42 AC 263 ms
15,744 KB
testcase_43 AC 258 ms
14,976 KB
testcase_44 AC 223 ms
14,464 KB
testcase_45 AC 227 ms
14,464 KB
testcase_46 AC 230 ms
14,464 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