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

  1. #include<iostream>  
  2. #include<cstring>  
  3. #include<string>  
  4. #include<cstdio>  
  5. #include<algorithm>  
  6.   
  7. using namespace std;  
  8.   
  9. #define MAXN 100100  
  10.   
  11. int next[MAXN];  
  12. int v[MAXN],l[MAXN],r[MAXN],d[MAXN];  
  13. int root[MAXN];  
  14.   
  15. void start()  
  16. {  
  17.     for(int i=0;i<MAXN-10;i++)  
  18.         next[i]=i+1;  
  19. }  
  20.   
  21. int merge(int x,int y)  
  22. {  
  23.     if(!x) return y;  
  24.     if(!y) return x;  
  25.     if(v[x]<v[y])  
  26.         swap(x,y);  
  27.     r[x]=merge(r[x],y);  
  28.     if(d[l[x]]<d[r[x]])  
  29.         swap(l[x],r[x]);  
  30.     d[x]=d[r[x]]+1;  
  31.     return x;  
  32. }  
  33.   
  34. int init(int x)  
  35. {  
  36.     int p=next[0];  
  37.     next[0]=next[p];  
  38.     v[p]=x;  
  39.     l[p]=r[p]=d[p]=0;  
  40.     return p;  
  41. }  
  42.   
  43. void del(int x)  
  44. {  
  45.     next[x]=next[0];  
  46.     next[0]=x;  
  47. }  
  48.   
  49. int insert(int x,int y)  
  50. {  
  51.     return merge(x,init(y));  
  52. }  
  53.   
  54. int top(int x)  
  55. {  
  56.     return v[x];  
  57. }  
  58.   
  59. int pop(int x)  
  60. {  
  61.     int p=merge(l[x],r[x]);  
  62.     del(x);  
  63.     return p;  
  64. }  
  65.   
  66. int set[MAXN];  
  67.   
  68. void initUnionSet(int n)  
  69. {  
  70.     for(int i=0;i<=n;i++)  
  71.         set[i]=i;  
  72. }  
  73.   
  74. int find(int a)  
  75. {  
  76.   
  77.     int root=a;  
  78.     int temp;  
  79.     while(set[root]!=root)  
  80.         root=set[root];  
  81.     while(set[a]!=root)  
  82.     {  
  83.         temp=a;  
  84.         a=set[a];  
  85.         set[temp]=root;  
  86.     }  
  87.     return root;  
  88. }  
  89.   
  90. bool unions(int a,int b)  
  91. {  
  92.     int x=find(a);  
  93.     int y=find(b);  
  94.     if(x==y)  
  95.         return false;  
  96.     set[x]=y;  
  97.     return true;  
  98. }  
  99.   
  100. int n,m,t;  
  101.   
  102. int main()  
  103. {  
  104.     while(~scanf("%d",&n))  
  105.     {  
  106.         start();  
  107.         initUnionSet(n);  
  108.         for(int i=1;i<=n;i++)  
  109.         {  
  110.             int tmp;  
  111.             scanf("%d",&tmp);  
  112.             root[i]=0;  
  113.             root[i]=insert(root[i],tmp);  
  114.         }  
  115.         scanf("%d",&m);  
  116.         for(int i=0;i<m;i++)  
  117.         {  
  118.             int a,b;  
  119.             scanf("%d%d",&a,&b);  
  120.             if(find(a)==find(b))  
  121.                 printf("-1\n");  
  122.             else  
  123.             {  
  124.                 int x,y;  
  125.                 a=find(a);  
  126.                 b=find(b);  
  127.   
  128.                 x=top(root[a]);  
  129.                 root[a]=pop(root[a]);  
  130.                 root[a]=insert(root[a],x/2);  
  131.   
  132.                 y=top(root[b]);  
  133.                 root[b]=pop(root[b]);  
  134.                 root[b]=insert(root[b],y/2);  
  135.   
  136.                 unions(a,b);  
  137.                 root[b]=merge(root[a],root[b]);  
  138.   
  139.                 printf("%d\n",top(root[b]));  
  140.             }  
  141.         }  
  142.     }  
  143.     return 0;  
  144. }  

非手動分配



  1. #include<iostream>  
  2. #include<cstring>  
  3. #include<string>  
  4. #include<cstdio>  
  5. #include<algorithm>  
  6.   
  7. using namespace std;  
  8.   
  9. #define MAXN 1000100  
  10.   
  11. int tot,v[MAXN],l[MAXN],r[MAXN],d[MAXN];  
  12. int root[MAXN];  
  13.   
  14.   
  15. int merge(int x,int y)  
  16. {  
  17.     if(!x) return y;  
  18.     if(!y) return x;  
  19.     if(v[x]<v[y])  
  20.         swap(x,y);  
  21.     r[x]=merge(r[x],y);  
  22.     if(d[l[x]]<d[r[x]])  
  23.         swap(l[x],r[x]);  
  24.     d[x]=d[r[x]]+1;  
  25.     return x;  
  26. }  
  27.   
  28. int init(int x)  
  29. {  
  30.     tot++;  
  31.     v[tot]=x;  
  32.     l[tot]=r[tot]=d[tot]=0;  
  33.     return tot;  
  34. }  
  35.   
  36. int insert(int x,int y)  
  37. {  
  38.     return merge(x,init(y));  
  39. }  
  40.   
  41. int top(int x)  
  42. {  
  43.     return v[x];  
  44. }  
  45.   
  46. int pop(int x)  
  47. {  
  48.     return merge(l[x],r[x]);  
  49. }  
  50.   
  51. int set[MAXN];  
  52.   
  53. void initUnionSet(int n)  
  54. {  
  55.     for(int i=0;i<=n;i++)  
  56.         set[i]=i;  
  57. }  
  58.   
  59. int find(int a)  
  60. {  
  61.   
  62.     int root=a;  
  63.     int temp;  
  64.     while(set[root]!=root)  
  65.         root=set[root];  
  66.     while(set[a]!=root)  
  67.     {  
  68.         temp=a;  
  69.         a=set[a];  
  70.         set[temp]=root;  
  71.     }  
  72.     return root;  
  73. }  
  74.   
  75. bool unions(int a,int b)  
  76. {  
  77.     int x=find(a);  
  78.     int y=find(b);  
  79.     if(x==y)  
  80.         return false;  
  81.     set[x]=y;  
  82.     return true;  
  83. }  
  84.   
  85. int n,m,t;  
  86.   
  87. int main()  
  88. {  
  89.     while(~scanf("%d",&n))  
  90.     {  
  91.         initUnionSet(n);  
  92.         tot=0;  
  93.         for(int i=1;i<=n;i++)  
  94.         {  
  95.             int tmp;  
  96.             scanf("%d",&tmp);  
  97.             root[i]=0;  
  98.             root[i]=insert(root[i],tmp);  
  99.         }  
  100.         scanf("%d",&m);  
  101.         for(int i=0;i<m;i++)  
  102.         {  
  103.             int a,b;  
  104.             scanf("%d%d",&a,&b);  
  105.             if(find(a)==find(b))  
  106.                 printf("-1\n");  
  107.             else  
  108.             {  
  109.                 int x,y;  
  110.                 a=find(a);  
  111.                 b=find(b);  
  112.   
  113.                 x=top(root[a]);  
  114.                 root[a]=pop(root[a]);  
  115.                 root[a]=insert(root[a],x/2);  
  116.   
  117.                 y=top(root[b]);  
  118.                 root[b]=pop(root[b]);  
  119.                 root[b]=insert(root[b],y/2);  
  120.   
  121.                 unions(a,b);  
  122.                 root[b]=merge(root[a],root[b]);  
  123.   
  124.                 printf("%d\n",top(root[b]));  
  125.             }  
  126.         }  
  127.     }  
  128.     return 0;  
  129. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章