求调(玄关)
查看原帖
求调(玄关)
674967
xz001楼主2023/11/20 13:24
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m, p[100005];
priority_queue <pair <int, int>, vector <pair <int, int> >, greater <pair <int, int> > > q[100005];
int find (int x) {
	return x == p[x] ? x : p[x] = find(p[x]);
}
signed main() {
	scanf("%lld%lld", &n, &m);
	for (int i = 1, a; i <= n; ++ i) {
		scanf("%lld", &a);
		q[i].push({a, i});
		p[i] = i;
	}
	while (m -- ) {
		int op;
		scanf("%lld", &op);
		if (op == 1) {
			int x, y;
			scanf("%lld%lld", &x, &y);
			if (!find(x) || !find(y)) continue;
			if (find(x) == find(x)) continue;
			if (q[find(x)].size() < q[find(y)].size()) {
				int at = find(x), f = find(y);
				while (!q[at].empty()) {
					int d1 = q[at].top().first, d2 = q[at].top().second;
					q[at].pop();
					q[f].push({d1, d2});
					p[find(d2)] = f;
				}
			} else {
				int at = find(y), f = find(x);
				while (!q[at].empty()) {
					int d1 = q[at].top().first, d2 = q[at].top().second;
					q[at].pop();
					q[f].push({d1, d2});
					p[find(d2)] = f;
				}
			}
		} else {
			int x;
			scanf("%lld", &x);
			x = find(x);
			if (x == 0) {
				puts("-1");
				continue;
			}
			if (q[x].size()) {
				printf("%lld\n", q[x].top().first);
			    p[q[x].top().second] = 0;
			    q[x].pop();
			} else {
				printf("-1\n");
			}
		}
	}
	return 0;
} 

WA 28,求助大佬。

2023/11/20 13:24
加载中...