zoukankan      html  css  js  c++  java
  • HDU1512 Monkey King(左偏树)

    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. 

    InputThere 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. 

    OutputFor 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


    题解:左偏树板题;
    就是每次战斗后,两颗子树里面最大的元素减半;然后合并子树,这是左偏树的基本操作;
    就是加一个find(x)操作,就是找根,如果两个点的根相同,则输出-1;
    参考代码:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define clr(a,val) memset(a,val,sizeof(a))
     4 #define pb push_back
     5 #define fi first
     6 #define se second
     7 typedef long long ll;
     8 inline int read()
     9 {
    10     int x=0,f=1;char ch=getchar();
    11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    12     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    13     return x*f;
    14 }
    15 const int N=100005;
    16 int f[N],a[N],rs[N],ls[N],dis[N];
    17 int find(int x){if(f[x]!=x) f[x]=find(f[x]);return f[x];}
    18 int merge(int A,int B)//合并树 
    19 {
    20     if (!A||!B) return A+B;
    21     if(a[A]<a[B]) swap(A,B);
    22     rs[A]=merge(rs[A],B);
    23     if(dis[ls[A]]<dis[rs[A]]) swap(ls[A],rs[A]);
    24     dis[A]=dis[rs[A]]+1;
    25     return A;
    26 }
    27 int pop(int x)
    28 {
    29     f[x]=merge(ls[x],rs[x]);
    30     f[f[x]]=f[x];
    31     ls[x]=rs[x]=dis[x]=0;
    32     return f[x];
    33 }
    34 int main()
    35 {
    36     int n,m;
    37     while(~scanf("%d",&n))
    38     {
    39         clr(rs,0);clr(ls,0);clr(dis,0);
    40         for(int i=1;i<=n;++i) a[i]=read(),f[i]=i;
    41         m=read();
    42         while(m--)
    43         {
    44             int x,y;scanf("%d%d",&x,&y);
    45             x=find(x); y=find(y); int top;
    46             if(x==y){ printf("-1
    ");continue; }
    47             a[x]/=2; int fx=pop(x); top=merge(x,fx); f[fx]=f[x]=top; 
    48             a[y]/=2; int fy=pop(y); top=merge(y,fy); f[fy]=f[y]=top; 
    49             x=find(x); y=find(y); top=merge(x,y); f[x]=f[y]=top; 
    50             printf("%d
    ",a[top]);
    51         }    
    52     }
    53     return 0;
    54 }
    View Code
  • 相关阅读:
    一个关于css3背景透明的例子
    原生js写的一个当前年份日期星期和时间的显示
    一个原生js写的加减乘除运算
    风行一时瀑布流网页布局,实现无限加载(jquery)
    bootstrap你让前端小狮子们又喜又恨
    潭州课堂25班:Ph201805201 django 项目 第二十一课 文章主页 新闻列表页面功能 (课堂笔记)
    潭州课堂25班:Ph201805201 django 项目 第二十课 数据库分析设计图 (课堂笔记)
    潭州课堂25班:Ph201805201 django 项目 第十九课 文章主页数据库模型,前后台功能实现 (课堂笔记)
    潭州课堂25班:Ph201805201 django 项目 第十八课 前台 注解 (课堂笔记)
    潭州课堂25班:Ph201805201 django 项目 第十七课 用户登录,登出实现 (课堂笔记)
  • 原文地址:https://www.cnblogs.com/csushl/p/10293271.html
Copyright © 2011-2022 走看看