码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷P1456Monkey King

时间:2017-11-04 15:10:13      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:copy   value   begin   ssi   ica   sts   lines   gre   conf   

洛谷P1456 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.

n个猴子打了m次架,每打一次架,双方都会派出自己一方战斗力最强的猴子参战,打完架后,所派出的猴子的战斗力减半,同时两方猴子变为朋友,如果友方内部发生冲突,输出-1,每打一次架后,输出两方猴子合并后的最强战斗力。

输入输出格式

输入格式:

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.

输出格式: 

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strength value of the strongest monkey among all of its friends after the duel. 

输入输出样例

输入样例#1:
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
输出样例#1: 
8
5
5
-1
10

这是一道左偏树的模板题,我发现单靠并查集是做不了的,于是去了解了一下左偏树这个东西。
打架前,先判断两只猴子是不是属于同一方,若是,输出-1;若不是,直接用左偏树的merge函数合并两方猴子,同时维护左偏树,输出根节点即可。
 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 using namespace std;
 6 int n,m,x1,y;
 7 int f1,f2,u,v;
 8 struct node{
 9     int r;
10     int l;
11     int v;
12     int dis;
13     int f;
14 }tr[1000001];
15 int find(int x)
16 {
17     if(tr[x].f!=x) return find(tr[x].f);
18     return x;
19 }
20 int merge(int a,int b)//合并两方猴子
21 {
22     if(a==0) return b;
23     if(b==0) return a;
24     if(tr[a].v<tr[b].v) swap(a,b);//性质1:节点的键值小于或等于它的左右子节点的键值。
25     tr[a].r=merge(tr[a].r,b);//性质2:节点的左子节点右子节点也是一颗左偏树。
26     tr[tr[a].r].f=a;
27     if(tr[tr[a].l].dis<tr[tr[a].r].dis) swap(tr[a].l,tr[a].r);//性质:3:节点的左子节点的距离不小于右子节点的距离。
28     if(tr[a].r==0) tr[a].dis=0;
29     else tr[a].dis=tr[tr[a].r].dis+1;
30     return a;
31 }
32 int pop(int a)
33 {
34     int l=tr[a].l;
35     int r=tr[a].r;
36     tr[l].f=l;
37     tr[r].f=r;
38     tr[a].l=tr[a].r=tr[a].dis=0;
39     return merge(l,r);
40 }
41 int main()
42 {
43     while(scanf("%d",&n)!=EOF)//多组数据
44     {
45        for(int i=1;i<=n;i++)
46        {
47            scanf("%d",&tr[i].v);
48            tr[i].l=tr[i].r=tr[i].dis=0;
49            tr[i].f=i;
50        }
51        scanf("%d",&m);
52        for(int i=1;i<=m;i++)
53        {
54           scanf("%d%d",&x1,&y);
55           f1=find(x1);
56           f2=find(y);
57           if(f1==f2)
58           printf("-1\n");
59           else
60           {
61              tr[f1].v/=2;
62              u=pop(f1);
63              u=merge(u,f1);
64              tr[f2].v/=2;
65              v=pop(f2);
66              v=merge(v,f2);
67              printf("%d\n",tr[merge(u,v)].v);
68           }
69        }
70     }
71     return 0;
72 }

 

洛谷P1456Monkey King

标签:copy   value   begin   ssi   ica   sts   lines   gre   conf   

原文地址:http://www.cnblogs.com/drurry/p/7783199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!