zoukankan      html  css  js  c++  java
  • 数据结构(左偏树):HDU 1512 Monkey King

    Monkey King

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4714    Accepted Submission(s): 2032


    Problem Description
    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 there 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
     
     
      注意初始值:dis[0]=-1……
      因为delete掉的点还要在用,所以在delete时要清空ls和rs……
      
      这两个细节是致命的。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int maxn=100010;
     6 int ch[maxn][2],dis[maxn],key[maxn];
     7 int fa[maxn],rt[maxn],n,m;
     8 void Init(){
     9     dis[0]=-1;
    10     for(int i=0;i<=n;i++){
    11         fa[i]=i;rt[i]=i;dis[i]=0;
    12         ch[i][0]=ch[i][1]=0;
    13     }
    14 }
    15 
    16 int Merge(int x,int y){
    17     if(!x||!y)return x|y;
    18     if(key[x]<key[y])swap(x,y);
    19     ch[x][1]=Merge(ch[x][1],y);
    20     if(dis[ch[x][1]]>dis[ch[x][0]])
    21         swap(ch[x][0],ch[x][1]);
    22     dis[x]=dis[ch[x][1]]+1;
    23     return x;    
    24 }
    25 
    26 void Delete(int x){
    27     int tmp=rt[x];
    28     rt[x]=Merge(ch[rt[x]][0],ch[rt[x]][1]);
    29     ch[tmp][0]=ch[tmp][1]=0;
    30 }
    31 
    32 int Find(int x){
    33     return x==fa[x]?x:fa[x]=Find(fa[x]);
    34 }
    35 
    36 int main(){
    37     while(scanf("%d",&n)!=-1){
    38         Init();
    39         for(int i=1;i<=n;i++)
    40             scanf("%d",&key[i]);
    41         int x,y;
    42         scanf("%d",&m);
    43         while(m--){
    44             scanf("%d%d",&x,&y);
    45             x=Find(x);y=Find(y);
    46             if(x==y)
    47                 printf("-1
    ");
    48             else{
    49                 int ta=rt[x],tb=rt[y];
    50                 Delete(x);key[ta]/=2;rt[x]=Merge(rt[x],ta);
    51                 Delete(y);key[tb]/=2;rt[y]=Merge(rt[y],tb);
    52                 fa[y]=x;rt[x]=Merge(rt[x],rt[y]);
    53                 printf("%d
    ",key[rt[x]]);
    54             }    
    55         }
    56     }
    57     return 0;
    58 }    
     
     
  • 相关阅读:
    Asp.net分页控件AspNetPager的使用(DataList和Reapter结合Linq的编辑与删除)
    Zend Framework使用Extjs进行数据库交互和分页
    课程设计之(struts2+Hibernate)航空订票系统
    课程设计之(ZendFrameWrok)构建的航空订票系统网站
    Ubuntu10.10 Zend FrameWork配置及helloworld显示
    ExtJs、Struts2、Hibernate3.2登录页面的简单实现
    Asp.net的FileUpload控件的文件上传与Extjs文件上传的简单Demo
    学习linux的一些网络资源
    100条超搞笑的“雷人”QQ/MSN 签名
    超难的75道逻辑思维题
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5612123.html
Copyright © 2011-2022 走看看