您的位置:首页 > 其它

BZOJ 1455 罗马游戏【可并堆+并查集

2016-11-29 17:31 316 查看
对于每个集合维护一个堆,merge就合并,注意已经死了的不会被操作……【因为这个wa了一年2333

总觉得1e6跑起来很虚?结果快的飞起

#include<bits/stdc++.h>
#define MAXN 1000005
using namespace std; int n,m;
inline int read(){
register char ch = getchar();
while(!isdigit(ch)) ch = getchar();
register int rtn = 0;
while(isdigit(ch)) rtn = rtn*10 + ch - '0' , ch = getchar();
return rtn;
}
//==========================================
struct Node{
Node *Son,*Brother;
int key,id;
Node(){}
Node(Node *Son,Node *Brother,int key,int id):
Son(Son),Brother(Brother),key(key),id(id){}
}t[MAXN],*root[MAXN],*null;
int cnt_node;
inline Node * New_node(Node *s,Node * b,int k,int i){
return &(t[++cnt_node] = Node(s,b,k,i));
}
inline Node * merge(Node *a,Node *b){
if(a==null) return b;
if(b==null) return a;
if(a->key > b->key) swap(a,b);
a->Brother = null;
b->Brother = a->Son;
a->Son = b;
return a;
}
inline Node * pop(Node *a){
queue<Node *> q;
for(Node * now = a->Son;now!=null;now = now->Brother)
q.push(now);
if(q.size() == 0u) return null;
while(q.size() > 1u){
Node *a = q.front() ; q.pop();
Node *b = q.front() ; q.pop();
q.push(merge(a,b));
}
return q.front();
}

int fth[MAXN];
int findfather(int x){return fth[x] = (x==fth[x]?x:findfather(fth[x]));}
int die[MAXN];

char opt[5];
int read_x,read_y;
int main(){
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
n = read();
null = new Node(0,0,0,0);

for(int i=1;i<=n;++i)
root[fth[i] = i] = New_node(null,null,read(),i);

m = read();
while(m--){
scanf("%s",opt);
if(opt[0]=='M'){
read_x = read() , read_y = read();
if(die[read_x]||die[read_y]) continue;
read_x = findfather(read_x) , read_y = findfather(read_y);
if(read_x==read_y) continue;
root[read_y] = merge(root[read_x] , root[read_y]);
fth[read_x] = read_y;
}
else{
int k = read();
if(die[k]){
puts("0");
continue;
}
read_x = findfather(k);
if(root[read_x] == null){
puts("0");
continue;
}
printf("%d\n",root[read_x]->key);
die[root[read_x]->id] = 1;
root[read_x] = pop(root[read_x]);
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: