結果

問題 No.2290 UnUnion Find
ユーザー furonfuron
提出日時 2023-05-15 21:27:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 404 ms / 2,000 ms
コード長 2,001 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 134,500 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-05-08 03:13:50
合計ジャッジ時間 21,991 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 342 ms
5,376 KB
testcase_03 AC 317 ms
8,704 KB
testcase_04 AC 316 ms
8,704 KB
testcase_05 AC 313 ms
8,704 KB
testcase_06 AC 317 ms
8,704 KB
testcase_07 AC 324 ms
8,832 KB
testcase_08 AC 323 ms
8,832 KB
testcase_09 AC 321 ms
8,832 KB
testcase_10 AC 323 ms
8,704 KB
testcase_11 AC 318 ms
8,704 KB
testcase_12 AC 337 ms
8,704 KB
testcase_13 AC 323 ms
8,832 KB
testcase_14 AC 332 ms
8,832 KB
testcase_15 AC 319 ms
8,832 KB
testcase_16 AC 316 ms
8,704 KB
testcase_17 AC 329 ms
8,704 KB
testcase_18 AC 322 ms
8,704 KB
testcase_19 AC 357 ms
9,472 KB
testcase_20 AC 402 ms
14,208 KB
testcase_21 AC 354 ms
5,376 KB
testcase_22 AC 340 ms
5,888 KB
testcase_23 AC 346 ms
6,016 KB
testcase_24 AC 376 ms
11,264 KB
testcase_25 AC 339 ms
5,376 KB
testcase_26 AC 399 ms
12,800 KB
testcase_27 AC 381 ms
11,776 KB
testcase_28 AC 348 ms
7,680 KB
testcase_29 AC 368 ms
10,752 KB
testcase_30 AC 354 ms
5,376 KB
testcase_31 AC 364 ms
10,240 KB
testcase_32 AC 341 ms
5,376 KB
testcase_33 AC 343 ms
7,936 KB
testcase_34 AC 404 ms
14,208 KB
testcase_35 AC 386 ms
12,672 KB
testcase_36 AC 339 ms
5,632 KB
testcase_37 AC 342 ms
7,040 KB
testcase_38 AC 333 ms
5,504 KB
testcase_39 AC 337 ms
6,144 KB
testcase_40 AC 390 ms
12,160 KB
testcase_41 AC 347 ms
5,376 KB
testcase_42 AC 347 ms
9,088 KB
testcase_43 AC 347 ms
8,832 KB
testcase_44 AC 324 ms
8,704 KB
testcase_45 AC 337 ms
8,832 KB
testcase_46 AC 336 ms
8,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <random>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
const int inf = (1 << 30) - 1;
const ll INF = 1LL << 60;
const int MOD = 1000000007;
//const int MOD = 998244353;

struct UnionFind {
	vector<int> par;
	vector<int> sizes;

	UnionFind(int n) : par(n), sizes(n, 1) {
		for (int i = 0; i < n; i++) par[i] = i;
	}

	int root(int x) {
		if (x == par[x]) return x;
		return par[x] = root(par[x]);
	}

	void unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y) return;

		if (sizes[x] < sizes[y]) swap(x, y);
		par[y] = x;
		sizes[x] += sizes[y];
	}

	bool same(int x, int y) { return root(x) == root(y); }

	int size(int x) { return sizes[root(x)]; }
};

int main() {
	int n, q;
	cin >> n >> q;
	UnionFind uf(n);
	set<int> node;
	for (int i = 0; i < n; i++) node.insert(i);

	for (int i = 0; i < q; i++) {
		int t, u, v;
		cin >> t;
		if (t == 1) {
			cin >> u >> v;
			u--; v--;
			if (!uf.same(u, v)) {
				int r1 = uf.root(u);
				int r2 = uf.root(v);
				uf.unite(u, v);
				if (uf.root(r1) != r1) node.erase(r1);
				if (uf.root(r2) != r2) node.erase(r2);
			}

		}
		else {
			cin >> v;
			v--;
			int r = uf.root(v);
			if (*node.begin() == r) {
				if (node.size() == 1) {
					cout << -1 << endl;
				}
				else {
					cout << *(next(node.begin())) + 1 << endl;
				}
			}
			else {
				cout << *node.begin() + 1 << endl;
			}
		}
	}

	return 0;
}
0