51pts求条qwwwwwwwwq
查看原帖
51pts求条qwwwwwwwwq
1033727
toolazy楼主2023/11/7 12:03
#include <iostream>
#include <bitset>
using namespace std;

#define MS 100005
int l[MS], r[MS], v[MS], dis[MS], fa[MS];
bitset<MS> del;

int merge(int x, int y) {
	if (x == 0)
		return y;
	if (y == 0)
		return x;
	if (v[x] > v[y])
		swap(x, y);
	r[x] = merge(r[x], y);
	fa[r[x]] = x;
	if (dis[l[x]] < dis[r[x]])
		swap(l[x], r[x]);
	dis[x] = dis[r[x]] + 1;
	return x;
}

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

int pop(int x) {
	if (del[x])
		return -1;
	int rt = root(x);
	fa[l[rt]] = l[rt];
	fa[r[rt]] = r[rt];
	merge(l[rt], r[rt]);
	del[rt] = true;
	return v[rt];
}

int n, m;
void input() {
	cin >> n >> m;
	for (int i = 1; i <= n; i ++) {
		cin >> v[i];
		fa[i] = i;
		dis[i] = 1;
	}
}

int main() {
	input();
	while (m --) {
		int command, x, y;
		cin >> command >> x;
		if (command - 1) {
			cout << pop(x) << endl;
		} else {
			cin >> y;
			if (del[x] || del[y])
				continue;
			int rx = root(x), ry = root(y);
			if (rx == ry)
				continue;
			merge(rx, ry);
		}
	}
	return 0;
}
```C++
不知道错哪里了qwq,[测评记录](https://www.luogu.com.cn/record/133611235),5~8WA,9~11MLE qwq
挑不出来qwq
2023/11/7 12:03
加载中...