求助配对堆51pts
查看原帖
求助配对堆51pts
551861
strcmp楼主2022/4/11 17:26

rt

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 4e5 + 10;
struct node {
	int son, sub;
	ll val;
}heap[N];
int cnt = 0, n, m, a, b, rt = 0;
#define son(x) (heap[x].son)
#define sub(x) (heap[x].sub)
#define val(x) (heap[x].val)
int unset[N]; bool vis[N];
inline int newnode(ll val) {
	heap[cnt].val = val;
	return cnt++;
}
int find(int x) {
	if (unset[x] == x)return x;
	return unset[x] = find(unset[x]);
}
inline int merge(int x, int y) {
	if (!x || !y)return x | y;
	if (val(y) < val(x))swap(x, y);
	sub(y) = son(x), son(x) = y;
	unset[y] = x;
	return x;
}
int merges(int x) {
	int s = sub(x), ss = sub(s);
	unset[x] = x, unset[s] = s, unset[ss] = ss;
	sub(x) = 0, sub(s) = 0;
	if (!x || !s)return x | s;
	return merge(merge(x, s), merges(ss));
}
inline int del(int x) { 
	x = find(x);
	unset[son(x)] = son(x); int k = son(x); son(x) = 0;
	return merges(k); 
}
inline int unmerge(int x, int y) {
	x = find(x), y = find(y);
	return unset[x] = unset[y] = merge(x, y);
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(); cout.tie();
	cin >> n >> m;
	for (int i = 1; i <= n; i++)cin >> heap[i].val, unset[i] = i;
	int a,x,y;
	while (m--) {
		cin >> a;
		if(a == 1){
			cin >> x >> y;
			if (vis[x] || vis[y] || find(x) == find(y))continue;
			else unmerge(x, y);
		}
		else{
			cin >> x;
			x = find(x);
			if(vis[x])cout<<-1<<endl;
			else{
				vis[x] = true;
				cout << heap[x].val << endl;
				del(x);
			}
		}
	}
	return 0;
}

目测判不合法的地方写挂了。

2022/4/11 17:26
加载中...