BZOJ 1455

STL的基本用法

(居然能空间卡过去= =!!!)

#include <cstdio>
#include <ext/pb_ds/priority_queue.hpp>
#include <functional>
#include <algorithm>
#include <utility>
__gnu_pbds::priority_queue<std::pair<int,int>,std::greater<std::pair<int,int> >,__gnu_pbds::pairing_heap_tag> hps[1000005];
int hpl;
struct denizer{
	struct{
		int fa,rk;
	} d[1000005];
	inline int find(int n){
		int p=n,f;
		while(d[p].fa){
			p=d[p].fa;
		}
		while(n!=p){
			f=d[n].fa;
			d[n].fa=p;
			n=f;
		}
		return p;
	}
	bool merge(int& a,int& b){
		a=find(a),b=find(b);
		if(a==b) return 0;
		if(d[a].rk<d[b].rk) std::swap(a,b);
		d[b].fa=a;
		if(d[b].rk==d[a].rk) ++d[a].rk;
		return 1;
	}
}djset;
char s[10];
bool kd[1000005];
int Q;
int main(){
int n,i,j,k;
	scanf("%d",&n);
	for(i=1;i<=n;++i){
		scanf("%d",&j);
		hps[i].push(std::make_pair(j,i));
	}
	scanf("%d",&Q);
	while(Q--){
		scanf("%s%d",s,&j);
		switch(s[0]){
			case 'M':
			scanf("%d",&k);
			if(kd[j]||kd[k]) break;
			if(djset.merge(j,k)){
				hps[j].join(hps[k]);
			}
			break;
			case 'K':
			if(kd[j]){
				printf("0\n");break;
			}
			j=djset.find(j);
			if(!hps[j].empty()) printf("%d\n",hps[j].top().first),kd[hps[j].top().second]=true,hps[j].pop();
			break;
		}
	}
	return 0;
}

直接改了我出题的标程...

posted @ 2015-05-08 17:16  zball  阅读(196)  评论(0编辑  收藏  举报