• hdu1512 Monkey King


    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

    正解:左偏树

    左偏树板子题。。每次删除树根,键值除以2后再插入树,并且将两棵树合并。


    //It is made by wfj_2048~
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #define inf 1<<30
    #define il inline
    #define RG register
    #define ll long long
    #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    
    using namespace std;
    
    struct left_tree{ int fa,ls,rs,dis,key; }ltree[100010];
    
    int x,n,m;
    
    il int gi(){
        RG int x=0,q=0; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
        if (ch=='-') q=1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
    }
    
    il int father(RG int x){ if (ltree[x].fa!=x) return father(ltree[x].fa); else return x; }
    
    il void build(RG int x,RG int k){ ltree[x]=(left_tree){x,0,0,0,k}; return; }
    
    il int merge(RG int x,RG int y){
        if (!x) return y; if (!y) return x; if (ltree[x].key<ltree[y].key) swap(x,y);
        ltree[x].rs=merge(ltree[x].rs,y); RG int &l=ltree[x].ls,&r=ltree[x].rs;
        ltree[l].fa=ltree[r].fa=x; if (ltree[l].dis<ltree[r].dis) swap(l,r);
        if (!r) ltree[x].dis=0; else ltree[x].dis=ltree[r].dis+1; return x;
    }
    
    il int del(RG int rt){
        RG int l=ltree[rt].ls,r=ltree[rt].rs; ltree[l].fa=l,ltree[r].fa=r;
        ltree[rt].dis=ltree[rt].ls=ltree[rt].rs=0; return merge(l,r);
    }
    
    il int query(RG int x,RG int y){
        RG int l=del(x),r=del(y); ltree[x].key>>=1,ltree[y].key>>=1;
        l=merge(l,x),r=merge(r,y),l=merge(l,r); return ltree[l].key;
    }
    
    il void work(){
        for (RG int i=1;i<=n;++i) x=gi(),build(i,x); m=gi();
        for (RG int i=1;i<=m;++i){
    	RG int u=gi(),v=gi(),a=father(u),b=father(v);
    	if (a==b){ printf("-1
    "); continue; }
    	printf("%d
    ",query(a,b));
        }
        return;
    }
    
    int main(){
        File("monkeyking");
        while (scanf("%d",&n)!=EOF) work();
        return 0;
    }
    


  • 相关阅读:
    Eclipse 项目导入 Android Studio 导致的乱码问题
    Android 系统服务
    Android 系统内核层与 Linux Kernel 的比较
    DPI 计算及速查表
    Android 引用文件(.db)的三种方式
    阅读记录(2017年1月)
    如何让电脑自动记录每次开关机时间
    使用VS2010编译Qt 5.6.1过程记录
    Windows无线网“无法连接到这个网络”的解决办法
    怎样在Windows资源管理器中添加右键菜单以及修改右键菜单顺序
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6416600.html
Copyright © 2020-2023  润新知