zoukankan      html  css  js  c++  java
  • HDU

    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

    可并堆裸题,没什么思维难度。只要学会了就会打了.

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<cstring>
    #include<stdlib.h>
    const int MANX=101000;
    using namespace std;
    int fa[MANX],r[MANX],l[MANX],v[MANX],d[MANX];
    int n,m;
    
    void cl(){
        for(int i=1;i<=n;i++) fa[i]=i;
        memset(r,0,sizeof(r));
        memset(l,0,sizeof(l));
        memset(v,0,sizeof(v));
    }
    
    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 lz=l[x],rz=r[x];
        l[x]=r[x]=d[x]=0;fa[lz]=lz,fa[rz]=rz;
        return merge(lz,rz);
    }
    
    int main(){
        while(scanf("%d",&n)!=EOF){
            cl();
            for(int i=1;i<=n;i++) scanf("%d",&v[i]);
            scanf("%d",&m);
            for(int i=1;i<=m;i++){
                int x,y;scanf("%d%d",&x,&y);
                int ll=find(x),rr=find(y);
                if(ll==rr){printf("-1
    ");}
                else{
                    v[ll]/=2,v[rr]/=2;
                    int lz=del(ll),rz=del(rr);
                    lz=merge(ll,lz),rz=merge(rz,rr);
                    printf("%d
    ",v[merge(lz,rz)]);
                }
            }
        }
    }

             

  • 相关阅读:
    POJ 2778(自动机+矩阵幂乘(字符串果然是个坑爹玩应!))
    CF357D(规律题,不好想出来!)
    HDU 4107(线段树 特殊懒惰标记)g++ TLE,c++才过(呜呜呜呜)
    hdu 3954(线段树的特殊lazy操作-更新时才遍历!)
    CF354C(思路+(左区间++,1+右区间--思想))
    hdu3851(dp+区间范围很大且有循环的处理方法)
    CF119D(字符串-哈希求解(KMP求了半天,结果哈希更简单!))
    CF 353D
    HDU 4760 字典树(题意比较难理解)
    hdu3649(取石子博弈+dp)
  • 原文地址:https://www.cnblogs.com/renjianshige/p/7266929.html
Copyright © 2011-2022 走看看