ZOJ 2334(Monkey King-左偏树第一题)

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


左偏树第一题。

root[i]表以i为根的并查集的左偏树的当前根

这题在怎么表示[i]所在树之根的问题上纠结了很久,后发现,只要将2样拆开就行了。。。。这样每次修改都是O(1)的


 

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<functional>

#include<cmath>

#include<cctype>

#include<cassert>

#include<climits>

using namespace std;

#define For(i,n) for(int i=1;i<=n;i++)

#define Rep(i,n) for(int i=0;i<n;i++)

#define Fork(i,k,n) for(int i=k;i<=n;i++)

#define ForD(i,n) for(int i=n;i;i--)

#define Forp(x) for(int p=pre[x];p;p=next[p])

#define RepD(i,n) for(int i=n;i>=0;i--)

#define MEM(a) memset(a,0,sizeof(a))

#define MEMI(a) memset(a,127,sizeof(a))

#define MEMi(a) memset(a,128,sizeof(a))

#define INF (2139062143)

#define F (1000000009)

#define MAXN (101000+10)

#define MAXM (101000+10)

typedef long long ll;

struct node

{

   int v,ch[2],dis;

   node():v(0),dis(0){ch[0]=ch[1]=0;}

}q[MAXN];

int father[MAXN],root[MAXN];// root[i]表示以i为根的并查集的左偏树的当前根 

int merge(int a,int b)

{

   if (a*b==0) return a+b;

   if (q[a].v<q[b].v) swap(a,b);

   q[a].ch[1]=merge(q[a].ch[1],b);

   if (q[q[a].ch[0]].dis<q[q[a].ch[1]].dis) swap(q[a].ch[0],q[a].ch[1]);

   if (q[a].ch[1]) q[a].dis=q[q[a].ch[1]].dis+1;else q[a].dis=0;

   return a; 

}

int pop(int a)

{

   int p=merge(q[a].ch[0],q[a].ch[1]);

   q[a].dis=q[a].ch[0]=q[a].ch[1]=0;q[a].v/=2;

   int x=merge(a,p);

   return x;   

}

int getfather(int x)

{

   if (father[x]==x) return x;

   return father[x]=getfather(father[x]);

}

int n,m;

int main()

{

   //freopen("zoj2334.in","r",stdin);

   

   while(scanf("%d",&n)==1)

   {

      For(i,n) q[i]=node();

      For(i,n) scanf("%d",&q[i].v),father[i]=root[i]=i;

      scanf("%d",&m);

      For(i,m)

      {

      // For(i,n) cout<<getfather(i)<<' ';cout<<endl;

         int u,v,fu,fv;

         scanf("%d%d",&u,&v);

         if ((fu=getfather(u))==(fv=getfather(v))) printf("-1\n");

         else

         {

            int ru=root[fu],rv=root[fv];

            ru=pop(ru);rv=pop(rv);

            ru=merge(ru,rv); printf("%d\n",q[ru].v);

            father[fu]=fv;root[fv]=ru;

         }

      }

   // break;

   }

   

   //while(1);

   return 0;

}


 




 

你可能感兴趣的:(key)