hdu 1512 左偏树

题目:

Monkey King

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


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


分析:

每一只猴子看做一个结点,建立左偏树,树根维护最大值。若两结点树根相同,说明两只猴子是朋友,输出-1;

否则,两根节点值减半,删除,重新与原树合并得两新树,两新树合并后返回根节点值。

本题仅涉及到左偏树部分操作。



左偏树入门:

http://wenku.baidu.com/view/1c18eff9aef8941ea76e051b.html

http://wenku.baidu.com/view/004aa1ee19e8b8f67c1cb9d4.html?from=search

http://sunmoon-template.blogspot.ca/2014_12_01_archive.html


代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<ctype.h>    //tower()
#include<set>
#include<iomanip>// cout<<setprecision(1)<<fixed<<a;
#include<vector>   
#include<time.h>  
#include<assert.h>  //assert
#include<cmath>	
#include<algorithm>
#include<bitset>
#include<limits.h>
#include<stack>
#include<queue>
using namespace std;
const int maxn=100050;
const int inf=INT_MAX-100;

struct node{
    int val,dis,lt,rt,fa;//fa 父节点
}ltree[maxn];

int n,q,t;

void maketree(int a,int k){//结点成树 a结点值为k 
    ltree[a].val=k;
    ltree[a].fa=a;
    ltree[a].lt=ltree[a].rt=0;
    ltree[a].dis=(a==0)?-1:0;
}

int find(int x){//非递归路径压缩,返回所在树根节点 
    int s,k,j;
    s=k=x;
    while(s!=ltree[s].fa) s=ltree[s].fa;
    while(k!=s){
        j=ltree[k].fa;
        ltree[k].fa=s;
        k=j;
    }
    return s;
}

int merge(int a,int b){//两棵树合并 改变rt,dis,fa 
    if(a==0) return b;
    if(b==0) return a;
    if(ltree[a].val<ltree[b].val) swap(a,b);
    ltree[a].rt=merge(ltree[a].rt,b);//b接在a最右边 
	int &l=ltree[a].lt,&r=ltree[a].rt;//左右子树的引用,取别名,不另占内存,值同时修改    
    ltree[r].fa=a;//ltree[l].fa=
    if(ltree[l].dis<ltree[r].dis) swap(l,r);//维持左偏性 
    if(r==0) ltree[a].dis=0;
	else ltree[a].dis=ltree[r].dis+1;
    return a;
}

int solve(int a,int b){
	int tmp,ta,tb;
	a=find(a);
	b=find(b);
	if(a==b) return -1;
//	ltree[ltree[a].lt].fa=ltree[a].lt;
//	ltree[ltree[a].rt].fa=ltree[a].rt;
	ltree[a].val>>=1;
	tmp=merge(ltree[a].lt,ltree[a].rt);
	ltree[a].dis=ltree[a].lt=ltree[a].rt=0;
    ta=merge(tmp,a);
//	ltree[ltree[b].lt].fa=ltree[b].lt;
//	ltree[ltree[b].rt].fa=ltree[b].rt;
	ltree[b].val>>=1; 
	tmp=merge(ltree[b].lt,ltree[b].rt);
	ltree[b].dis=ltree[b].lt=ltree[b].rt=0;
    tb=merge(tmp,b);
    
    tmp=merge(ta,tb);
    ltree[tmp].fa=ltree[ta].fa=ltree[tb].fa=tmp;
    return ltree[tmp].val;
}

int main(){//795MS	3540K
    while(~scanf("%d",&n)){
	    for(int i=1;i<=n;++i){
	        scanf("%d",&t);
	        maketree(i,t);
	    }
//	    maketree(0,0);
	    scanf("%d",&q);
	    int c,d;
	    for(int i=0;i<q;++i){
	        scanf("%d%d",&c,&d);
	        printf("%d\n",solve(c,d));
	    }
    }
    return 0;    
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值