zoukankan      html  css  js  c++  java
  • [HDU1512]:Monkey King

    Monkey King

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

    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
    大意{
      有n只猴子,每只猴子有一个值,两只猴子如果打架的话,他们的值各自掉一半。
      给出m个事件,每个事件给出两只猴子,如果两只猴子不认识的话,打完架就成为了朋友
      (两只猴子的各自朋友也都互相成为了朋友),求出打完架后两只猴子的所有朋友中值最大的。
    }

    思路{嗯,这个是可并堆的裸题,左偏树的时间空间复杂度都比较低,还是非常不错的}

    #include<bits/stdc++.h>
    #define RG register
    #define il inline 
    #define N 100010
    using namespace std;
    int fa[N],d[N],l[N],r[N],n,v[N],k;
    void clear(){
      memset(d,0,sizeof(d));
      memset(l,0,sizeof(l));
      memset(r,0,sizeof(r));
      for(int i=1;i<=n;++i)fa[i]=i;
    }
    int find(int x){if(fa[x]!=x)fa[x]=find(fa[x]);return fa[x];}
    int merge(int x,int y){
      if(!x)return y;
      if(!y)return x;
      if(v[x]<v[y])swap(x,y);
      r[x]=merge(r[x],y);fa[r[x]]=x;
      if(d[r[x]]>d[l[x]])swap(l[x],r[x]);
      else d[x]=d[r[x]]+1;return x;
    }
    int del(int x){
      int le=l[x],ri=r[x];
      r[x]=l[x]=d[x]=0;fa[le]=le,fa[ri]=ri;
      return merge(le,ri);
    }
    int main(){
      while(scanf("%d",&n)!=EOF){
        clear();for(int i=1;i<=n;++i)scanf("%d",&v[i]);
        scanf("%d",&k);
        for(int i=1;i<=k;++i){
          int x,y;scanf("%d%d",&x,&y);
          x=find(x),y=find(y);
          if(x==y)cout<<"-1
    ";
          else {
    	v[x]>>=1,v[y]>>=1;
    	int le=del(x),ri=del(y);
    	le=merge(le,x),ri=merge(ri,y);
    	int rt=merge(le,ri);printf("%d
    ",v[rt]);
          }
        }
      }
      return 0;
    }
    

      

  • 相关阅读:
    linux安装kafka教程
    linux 系统java相关部署
    redies学习总结
    Sentinel自定义异常降级-新旧版本差异
    Android Bitmap压缩详解
    Head First之策略模式
    go测试
    go获取命令行参数
    JVM-垃圾收集算法基础
    Java代理模式
  • 原文地址:https://www.cnblogs.com/zzmmm/p/7223647.html
Copyright © 2011-2022 走看看