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\n",-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\n",a[t]);
66             }
67         }
68     }
69 }

 

转载于:https://www.cnblogs.com/fengzhiyuan/p/7471103.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值