【BZOJ2733】[HNOI2012]永无乡

题意:n个有权值的点,每次连边或查询某个点所在的连通块中权值第k大的点的编号(不存在输出-1)

思路1:每个点建一个可并堆,每次连边合并两个可并堆(记录一下size),递归查询即可

#include <cstdio>
#define inf 0x7fffff
#define N 3000009
using namespace std;
int number=0;
int n,m,q;
int f[N],father[N],left[N],right[N],root[N],a[N];
char ch;
int merge(int x,int y,int l,int r)
{
	if (!x||!y) return x|y;
	int mid=(l+r)>>1;
	left[x]=merge(left[x],left[y],l,mid);
	right[x]=merge(right[x],right[y],mid,r);
	f[x]+=f[y];
	return x;
}
char get()
{
	char ch=getchar();
	while (ch!='Q'&&ch!='B') 
		ch=getchar();
	return ch;
}
void build(int &y,int l,int r,int x)
{
	if (!y) y=++number;
	f[y]++;
	if (l+1==r) return;
	int mid=(l+r)>>1;
	if (x<=mid)
		build(left[y],l,mid,x);
	else
		build(right[y],mid,r,x);
}
int query(int x,int l,int r,int k)
{
	if (l+1==r) return l+1;
	int mid=(l+r)>>1;
	if (f[left[x]]>=k)
		return query(left[x],l,mid,k);
	else
		return query(right[x],mid,r,k-f[left[x]]);
}
int getfather(int x)
{
	return father[x]==x?x:father[x]=getfather(father[x]);
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		a[x]=i;
		father[i]=i;
		build(root[i],0,n,x);
	}
	for (int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		x=getfather(x);
		y=getfather(y);
		if (x!=y)
			root[x]=merge(root[x],root[y],0,n),father[y]=x;
	}
	scanf("%d",&q);
	for (int i=1;i<=q;i++)
	{
		ch=get();
		int x,y;
		scanf("%d%d",&x,&y);
		if (ch=='Q')
		{
			x=getfather(x);
			if (f[root[x]]<y)
				printf("-1\n");
			else
				printf("%d\n",a[query(root[x],0,n,y)]);
		}
		if (ch=='B')
		{
			x=getfather(x);
			y=getfather(y);
			if (x!=y)
				root[x]=merge(root[x],root[y],0,n),father[y]=x;
		}
	}
	return 0;
}

思路2:维护权值平衡树(splay-tree),连边时暴力合并,size小的一个一个拆下来,加入size大的上去。查询时同理。。(现在补上啦)

速度上没有差很多(前面2400ms,这个3600ms)

#include <bits/stdc++.h>
#define gc getchar()
#define N 100009
using namespace std;
int n,m,rank[N],x,y,w,q[N],rt,Q,father[N];
struct node
{
	int pnt,ch[2],sz,rank;
}a[N];
void up(int x)
{
	a[x].sz=a[a[x].ch[0]].sz+a[a[x].ch[1]].sz+1;
}
void rotate(int x)
{
	int y=a[x].pnt,z=a[y].pnt,l=(a[y].ch[1]==x),r=l^1;
	a[y].ch[l]=a[x].ch[r];
	if (a[x].ch[r]) a[a[x].ch[r]].pnt=y;
	a[x].pnt=z;
	if (z) a[z].ch[a[z].ch[1]==y]=x;
	a[x].ch[r]=y,a[y].pnt=x;
	up(y),up(x);
}
void splay(int x,int target,int y=0)
{
	for (;(y=a[x].pnt)!=target;rotate(x))
		if (a[y].pnt!=target)
			rotate(a[a[y].pnt].ch[0]==y^a[y].ch[0]==x?x:y);
}
int ins(int x,int y)
{
	int now=y;
	while (a[now].ch[a[x].rank>a[now].rank])
		a[now].sz++,now=a[now].ch[a[x].rank>a[now].rank];
	a[now].ch[a[x].rank>a[now].rank]=x;
	a[now].sz++;
	a[x].pnt=now,a[x].ch[0]=a[x].ch[1]=0;
	splay(x,0);
	return x;
}
void dfs(int x)
{
	q[++w]=x;
	if (a[x].ch[0]) dfs(a[x].ch[0]);
	if (a[x].ch[1]) dfs(a[x].ch[1]);
}
int getfather(int x)
{
	return father[x]==x?x:father[x]=getfather(father[x]);
}
void merge(int x,int y)
{
	if (getfather(x)==getfather(y)) return;
	splay(x,0),splay(y,0);
	if (a[x].sz<a[y].sz) swap(x,y);
	w=0,rt=x;
	dfs(y);
	for (int i=1;i<=w;i++)
		rt=ins(q[i],rt);
	father[getfather(x)]=getfather(y);
}
int qry(int x,int y)
{
	int now=x,num=y;
	while (num)
	{
		if (num==a[a[now].ch[0]].sz+1) num=0;
		else
			if (num>a[a[now].ch[0]].sz+1)
				num-=a[a[now].ch[0]].sz+1,now=a[now].ch[1];
			else now=a[now].ch[0];
	}
	return now;
}
int read()
{
	int x=1;
	char ch;
	while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1;
	int s=ch-'0';
	while (ch=gc,ch<='9'&&ch>='0') s=s*10+ch-'0';
	return s*x;
}
int main()
{
	n=read(),m=read();
	for (int i=1;i<=n;i++)
	{
		father[i]=i;
		a[i].rank=read();
		a[i].pnt=a[i].ch[0]=a[i].ch[1]=0;
		a[i].sz=1;
	}
	for (int i=1;i<=m;i++)
	{
		x=read(),y=read();
		merge(x,y);
	}
	Q=read();
	for (int i=1;i<=Q;i++)
	{
		char ch;
		while (ch=gc,ch!='Q'&&ch!='B');
		x=read(),y=read();
		if (ch=='Q')
		{
			splay(x,0);
			if (a[x].sz<y) puts("-1");
			else printf("%d\n",qry(x,y));
		}
		else merge(x,y);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值