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

80 篇文章 0 订阅
4 篇文章 0 订阅

对于每个集合维护一个堆,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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值