ZOJ2334 Monkey King 左偏树+并查集

有一n个猴子,每个猴子有一个战斗力,如果x和y不认识那么x会找他认识的猴子中战斗力最高的猴子与y认识的猴子中战斗力最高的猴子打一架,然后两只打架的猴子战斗力减半,然后x认得所有猴子就和y认识的所有猴子认识了,下次就不打架了。。。。。然后要我们输出打完架之后,在这个新的团体中战斗力最高的猴子的战斗力。

要快速查找最大值,然后要合并两个堆,那就选用左偏树。


因为我用了pop操作,发现,内存需要额外增加很多(但其实这道题目,节点弹出后会重新加入,也就是节点总数不变,其实可以pop之后,拿原来的节点在此插入,但我还是额外压了一个内存,手动分配。。。。从800MS激增到1500MS)

用并查集维护连通分量,如果x和y不连通,则按规则输出,然后合并。否则输出-1


Monkey King

Time Limit: 10 Seconds       Memory Limit: 32768 KB

Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can't avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of their friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.

Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).

And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.


Input

There are several test cases, and each case consists of two parts.

First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).

Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.


Output

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.

Sample Input

5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5

Sample Output

8
5
5
-1
10



手动内存分配的代码 1560ms

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>

using namespace std;

#define MAXN 100100

int next[MAXN];
int v[MAXN],l[MAXN],r[MAXN],d[MAXN];
int root[MAXN];

void start()
{
    for(int i=0;i<MAXN-10;i++)
        next[i]=i+1;
}

int merge(int x,int y)
{
    if(!x) return y;
    if(!y) return x;
    if(v[x]<v[y])
        swap(x,y);
    r[x]=merge(r[x],y);
    if(d[l[x]]<d[r[x]])
        swap(l[x],r[x]);
    d[x]=d[r[x]]+1;
    return x;
}

int init(int x)
{
    int p=next[0];
    next[0]=next[p];
    v[p]=x;
    l[p]=r[p]=d[p]=0;
    return p;
}

void del(int x)
{
    next[x]=next[0];
    next[0]=x;
}

int insert(int x,int y)
{
    return merge(x,init(y));
}

int top(int x)
{
    return v[x];
}

int pop(int x)
{
    int p=merge(l[x],r[x]);
    del(x);
    return p;
}

int set[MAXN];

void initUnionSet(int n)
{
	for(int i=0;i<=n;i++)
		set[i]=i;
}

int find(int a)
{

	int root=a;
	int temp;
	while(set[root]!=root)
		root=set[root];
	while(set[a]!=root)
	{
		temp=a;
		a=set[a];
		set[temp]=root;
	}
	return root;
}

bool unions(int a,int b)
{
	int x=find(a);
	int y=find(b);
	if(x==y)
		return false;
	set[x]=y;
	return true;
}

int n,m,t;

int main()
{
    while(~scanf("%d",&n))
    {
        start();
        initUnionSet(n);
        for(int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            root[i]=0;
            root[i]=insert(root[i],tmp);
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(find(a)==find(b))
                printf("-1\n");
            else
            {
                int x,y;
                a=find(a);
                b=find(b);

                x=top(root[a]);
                root[a]=pop(root[a]);
                root[a]=insert(root[a],x/2);

                y=top(root[b]);
                root[b]=pop(root[b]);
                root[b]=insert(root[b],y/2);

                unions(a,b);
                root[b]=merge(root[a],root[b]);

                printf("%d\n",top(root[b]));
            }
        }
    }
    return 0;
}

非手动分配



#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>

using namespace std;

#define MAXN 1000100

int tot,v[MAXN],l[MAXN],r[MAXN],d[MAXN];
int root[MAXN];


int merge(int x,int y)
{
    if(!x) return y;
    if(!y) return x;
    if(v[x]<v[y])
        swap(x,y);
    r[x]=merge(r[x],y);
    if(d[l[x]]<d[r[x]])
        swap(l[x],r[x]);
    d[x]=d[r[x]]+1;
    return x;
}

int init(int x)
{
    tot++;
    v[tot]=x;
    l[tot]=r[tot]=d[tot]=0;
    return tot;
}

int insert(int x,int y)
{
    return merge(x,init(y));
}

int top(int x)
{
    return v[x];
}

int pop(int x)
{
    return merge(l[x],r[x]);
}

int set[MAXN];

void initUnionSet(int n)
{
	for(int i=0;i<=n;i++)
		set[i]=i;
}

int find(int a)
{

	int root=a;
	int temp;
	while(set[root]!=root)
		root=set[root];
	while(set[a]!=root)
	{
		temp=a;
		a=set[a];
		set[temp]=root;
	}
	return root;
}

bool unions(int a,int b)
{
	int x=find(a);
	int y=find(b);
	if(x==y)
		return false;
	set[x]=y;
	return true;
}

int n,m,t;

int main()
{
    while(~scanf("%d",&n))
    {
        initUnionSet(n);
        tot=0;
        for(int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            root[i]=0;
            root[i]=insert(root[i],tmp);
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(find(a)==find(b))
                printf("-1\n");
            else
            {
                int x,y;
                a=find(a);
                b=find(b);

                x=top(root[a]);
                root[a]=pop(root[a]);
                root[a]=insert(root[a],x/2);

                y=top(root[b]);
                root[b]=pop(root[b]);
                root[b]=insert(root[b],y/2);

                unions(a,b);
                root[b]=merge(root[a],root[b]);

                printf("%d\n",top(root[b]));
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值