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.

    不知道题意怎么办,那就百度翻译吧,。。。。。。。

    算了我来说一下题解:这道题意思是猴子打架的话,就会找他们认识的猴子中最大的来打,然后战斗力减半,然后再合并,最终输出

    总共合并后的树上,最大值(也就是堆顶)。

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cmath>
     5 #include<cstring>
     6 using namespace std;
     7 
     8 const int NN=1e5+7;
     9 
    10 int n,m,a[NN];
    11 int r[NN],l[NN],d[NN],fa[NN];
    12 bool died[NN];
    13 
    14 int find(int num)
    15 {
    16     if (fa[num]!=num) return find(fa[num]);
    17     else return num;
    18 }
    19 int merge(int x,int y)
    20 {
    21     if (!x) return y;
    22     if (!y) return x;
    23     if (a[x]<a[y]) swap(x,y);
    24     r[x]=merge(r[x],y);
    25     fa[r[x]]=x;
    26     if (d[r[x]]>d[l[x]]) swap(r[x],l[x]);
    27     d[x]=d[r[x]]+1;
    28     return x;
    29 }
    30 int main()
    31 {
    32     //freopen("1.in","r",stdin);
    33     //freopen("fzy.out","w",stdout);
    34     d[0]=-1;
    35     while (~scanf("%d",&n))
    36     {
    37         memset(l,0,sizeof(r));
    38         memset(r,0,sizeof(r));
    39         for (int i=1;i<=n;i++)
    40         {
    41             fa[i]=i;
    42             scanf("%d",&a[i]);
    43         }
    44         scanf("%d",&m);
    45         for (int i=1;i<=m;i++)
    46         {
    47             int k,u,v;
    48             scanf("%d%d",&u,&v);
    49             int x=find(u),y=find(v);
    50             if (x==y) printf("%d
    ",-1);
    51             else
    52             {
    53                 fa[l[x]]=l[x],fa[r[x]]=r[x];54                 fa[l[y]]=l[y],fa[r[y]]=r[y];//先各个独立
    55                 a[x]/=2,a[y]/=2;
    56                 int t1=merge(l[x],r[x]);
    57                 int t2=merge(l[y],r[y]);
    58                 fa[t1]=t1,fa[t2]=t2;
    59                 l[x]=l[y]=r[x]=r[y]=0;//拆开
    60                 t1=merge(t1,x);
    61                 t2=merge(t2,y);
    62                 fa[t1]=t1,fa[t2]=t2;
    63                 int t=merge(t1,t2);//再合并
    64                 fa[t]=t;
    65                 printf("%d
    ",a[t]);
    66             }
    67         }
    68     }
    69 }
  • 相关阅读:
    第七周-学习进度条
    《梦断代码》阅读笔记01
    第六周-学习进度条
    构建之法阅读笔记03
    结对项目开发(石家庄地铁乘车系统)
    第五周-学习进度条
    第四周-学习进度条
    HDU--1272 小希的迷宫(并查集判环与联通)
    HDU--1856 More is better(简单带权并查集)
    HDU--3635 带权并查集
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7471103.html
Copyright © 2011-2022 走看看