zoukankan      html  css  js  c++  java
  • 【HNOI2012】永无乡(splay,启发式合并)

    题解

    Description

    永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示。某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛。如果从岛 a 出发经过若干座(含 0 座)桥可以到达岛 b,则称岛 a 和岛 b 是连 通的。现在有两种操作:B x y 表示在岛 x 与岛 y 之间修建一座新桥。Q x k 表示询问当前与岛 x连通的所有岛中第 k 重要的是哪座岛,即所有与岛 x 连通的岛中重要度排名第 k 小的岛是哪 座,请你输出那个岛的编号。

    Input

    第一行是用空格隔开的两个正整数 n 和 m,分别 表示岛的个数以及一开始存在的桥数。
    接下来的一行是用空格隔开的 n 个数,依次描述从岛 1 到岛 n 的重要度排名。随后的 m 行每行是用空格隔开的两个正整数 ai 和 bi,表示一开始就存 在一座连接岛 ai 和岛 bi 的桥。后面剩下的部分描述操作,该部分的第一行是一个正整数 q, 表示一共有 q 个操作,接下来的 q 行依次描述每个操作,操作的格式如上所述,以大写字母 Q 或B 开始,后面跟两个不超过 n 的正整数,字母与数字以及两个数字之间用空格隔开。
    对于 20%的数据 n≤1000,q≤1000

    对于 100%的数据 n≤100000,m≤n,q≤300000

    Output

    对于每个 Q x k 操作都要依次输出一行,其中包含一个整数,表 示所询问岛屿的编号。如果该岛屿不存在,则输出-1。

    Sample Input

    5 1
    4 3 2 5 1
    1 2
    7
    Q 3 2
    Q 2 1
    B 2 3
    B 1 5
    Q 2 1
    Q 2 4
    Q 2 3

    Sample Output

    -1
    2
    5
    1
    2

    题解

    首先对于连通性,很显然维护一个并查集来考虑。
    那么,现在的问题在于如何查询联通块第k大
    这个玩意显然是个动态区间,而且大小还会变化,所以考虑使用平衡树维护。
    那么,构建N棵splay,每次联通两个不连通的块的时候,直接启发式合并暴力合并,每个点最多被合并logn次,因此复杂度是正确的。
    如果两个点已经属于同一个联通块,那么不需要再次合并,可以直接忽略操作。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<algorithm>
    using namespace std;
    #define MAX 500000
    inline int read()
    {
    	register int x=0,t=1;
    	register char ch=getchar();
    	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    	if(ch=='-'){t=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    	return x*t;
    }
    struct Node
    {
    	int ch[2];
    	int val,ff,size;
    }t[MAX];
    int f[MAX];
    int root[MAX],tot;
    int hh[MAX];
    int N,M;
    int getf(int x)
    {
    	return x==f[x]?x:f[x]=getf(f[x]);
    }
    inline void pushup(int x)
    {
    	t[x].size=t[t[x].ch[0]].size+t[t[x].ch[1]].size+1;
    }
    //1..N分别为N棵splay的0节点
    //每次都对splay进行合并
    inline void rotate(int x)
    {
    	int y=t[x].ff;
    	int z=t[y].ff;
    	int k=t[y].ch[1]==x;
    	t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
    	t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
    	t[x].ch[k^1]=y;t[y].ff=x;
    	pushup(y);pushup(x);
    }
    inline void splay(int x,int goal)
    {
    	while(t[x].ff!=goal)
    	{
    		int y=t[x].ff,z=t[y].ff;
    		if(z!=goal)
    			(t[z].ch[0]==y)^(t[y].ch[0]==x)?rotate(x):rotate(y);
    		rotate(x);
    	}
    	if(goal<=N)root[goal]=x;//如果是某一个0节点的下方,则更新当前splay的根节点
    }
    inline void insert(int x,int bh)
    {
    	int u=root[bh],ff=bh;
    	while(u&&t[u].val!=x)
    		ff=u,u=t[u].ch[x>t[u].val];
    	u=++tot;
    	t[u].size=1;
    	t[u].ff=ff;
    	if(ff>N)
    		t[ff].ch[x>t[ff].val]=u;
    	t[u].val=x;t[u].ch[0]=t[u].ch[1]=0;
    	splay(u,bh);
    }
    void DFS(int u,int kk)//遍历整颗splay
    {
    	if(t[u].ch[0])DFS(t[u].ch[0],kk);
    	if(t[u].ch[1])DFS(t[u].ch[1],kk);
    	insert(t[u].val,kk);//合并到另外一颗splay中
    }
    inline void Merge(int a,int b)
    {
    	int x=getf(a),y=getf(b);
    	if(x==y)return;//已经在一个集合内
    	if(t[root[x]].size>t[root[y]].size)swap(x,y);//强制将小的合并到大的
    	f[x]=y;
    	DFS(root[x],y);
    }
    int kth(int bh,int k)
    {
    	int u=root[bh];
    	if(t[u].size<k)return -1;
    	while(233)
    	{
    		if(t[t[u].ch[0]].size+1<k)//在右子树中找
    		{
    			k-=t[t[u].ch[0]].size+1;
    			u=t[u].ch[1];
    		}
    		else
    			if(t[t[u].ch[0]].size>=k)//在左子树中找
    				u=t[u].ch[0];
    			else
    				return t[u].val;//当前节点
    	}
    }
    int main()
    {
    	N=read();M=read();
    	for(int i=1;i<=N;++i)root[i]=i+N,f[i]=i;
    	tot=N+N;
    	for(int i=1;i<=N;++i)
    	{
    		int x=read();
    		hh[x]=i;
    		t[i+N].val=x;t[i+N].size=1;t[i+N].ff=i;
    	}
    	for(int i=1;i<=M;++i)
    	{
    		int x=read(),y=read();
    		Merge(x,y);
    	}
    	int Q=read();
    	while(Q--)
    	{
    		char ch[3];int a,b;
    		scanf("%s",ch);a=read(),b=read();
    		if(ch[0]=='B')
    		{
    			Merge(a,b);
    		}
    		else
    		{
    			int ans=kth(getf(a),b);
    			printf("%d
    ",ans==-1?ans:hh[ans]);
    		}
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    urllib使用四--urlencode,urlparse,
    urllib使用三--urlretrieve下载文件
    urllib使用二
    urllib使用一
    python使用网易邮箱发邮件
    python QQ邮件发送邮件
    可以字符串string转化成list,tuple,dict的eval()方法
    一行代码将两个列表拼接出第三个列表(两个可迭代对象相加产生第三个可迭代对象)--map()方法
    把列表中的元素拼接成字符串
    Runtime 类
  • 原文地址:https://www.cnblogs.com/cjyyb/p/7429519.html
Copyright © 2011-2022 走看看