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;
for (int i = 1; i <= n; i++)cin >> heap[i].val, unset[i] = i;
char a; int x, y;
cin >> m;
while (m--) {
cin >> a;
if(a == 'M'){
cin >> x >> y;
if (vis[x] || vis[y])continue;
else unmerge(x, y);
}
else{
cin >> x;
x = find(x);
if(vis[x])cout << 0 << endl;
else{
vis[x] = true;
cout << heap[x].val << endl;
del(x);
}
}
}
return 0;
}
这个板子模板+P2713都过不了,但MonkeyKing过了。