結果

問題 No.2290 UnUnion Find
ユーザー forest3forest3
提出日時 2023-05-09 12:49:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 347 ms / 2,000 ms
コード長 978 bytes
コンパイル時間 1,931 ms
コンパイル使用メモリ 175,632 KB
実行使用メモリ 13,312 KB
最終ジャッジ日時 2024-05-04 12:54:26
合計ジャッジ時間 21,979 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 302 ms
5,376 KB
testcase_03 AC 277 ms
8,448 KB
testcase_04 AC 278 ms
8,320 KB
testcase_05 AC 280 ms
8,320 KB
testcase_06 AC 280 ms
8,320 KB
testcase_07 AC 282 ms
8,320 KB
testcase_08 AC 281 ms
8,320 KB
testcase_09 AC 294 ms
8,320 KB
testcase_10 AC 342 ms
8,448 KB
testcase_11 AC 300 ms
8,320 KB
testcase_12 AC 316 ms
8,320 KB
testcase_13 AC 315 ms
8,320 KB
testcase_14 AC 274 ms
8,448 KB
testcase_15 AC 277 ms
8,320 KB
testcase_16 AC 278 ms
8,320 KB
testcase_17 AC 280 ms
8,320 KB
testcase_18 AC 306 ms
8,320 KB
testcase_19 AC 317 ms
9,216 KB
testcase_20 AC 347 ms
13,312 KB
testcase_21 AC 318 ms
5,376 KB
testcase_22 AC 316 ms
5,880 KB
testcase_23 AC 339 ms
5,876 KB
testcase_24 AC 334 ms
10,752 KB
testcase_25 AC 321 ms
5,376 KB
testcase_26 AC 336 ms
12,032 KB
testcase_27 AC 320 ms
11,264 KB
testcase_28 AC 308 ms
7,424 KB
testcase_29 AC 341 ms
10,112 KB
testcase_30 AC 311 ms
5,376 KB
testcase_31 AC 313 ms
9,472 KB
testcase_32 AC 303 ms
5,376 KB
testcase_33 AC 305 ms
7,448 KB
testcase_34 AC 343 ms
13,312 KB
testcase_35 AC 333 ms
11,904 KB
testcase_36 AC 311 ms
5,632 KB
testcase_37 AC 296 ms
6,812 KB
testcase_38 AC 324 ms
5,376 KB
testcase_39 AC 328 ms
6,144 KB
testcase_40 AC 326 ms
11,520 KB
testcase_41 AC 320 ms
5,376 KB
testcase_42 AC 300 ms
8,704 KB
testcase_43 AC 308 ms
8,448 KB
testcase_44 AC 283 ms
8,448 KB
testcase_45 AC 303 ms
8,320 KB
testcase_46 AC 293 ms
8,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct UnionFind {
  vector< int > data;
  set<int> st;
 
  UnionFind(int sz) {
    data.assign(sz, -1);
	for( int i = 0; i < sz; i++ ) st.insert( i );
  }
 
  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
	st.erase( y );
    return (true);
  }
 
  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }
 
  int size(int k) {
    return (-data[find(k)]);
  }
  int ex( int r ) {
	auto it = st.begin();
	while( *it == r ) it++;
	return *it;
  }
};

int main() {
	int N, Q;
	cin >> N >> Q;
	UnionFind uf( N );
	for( int i = 0; i < Q; i++ ) {
		int t, u, v;
		cin >> t >> u;
		u--;
		if( t == 1 ) {
			cin >> v;
			v--;
			uf.unite( u, v );
		}
		else {
			int ans = -1;
			int r = uf.find( u );
			if( uf.size( r ) < N ) ans = uf.ex( r ) + 1;
			cout << ans << endl;
		}
	}
}
0