hdu1512&洛谷p1456

原创 2018年04月15日 23:10:53

题目描述

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次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-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

说明

题目可能有多组数据








题解:左偏树模板题(PS:上次那个模板并不是太完整,不能处理删除一个点后再插入这个点的情况)


代码:

#include<bits/stdc++.h>
using namespace std;
int a[200000],ch[200000][2],f[200000],d[200000];
int merge(int x,int y){  
    if(!x||!y)return x+y;  
    if(a[x]<a[y])swap(x,y);  
    ch[x][1]=merge(ch[x][1],y);  
    f[ch[x][1]]=x;  
    if(d[ch[x][0]]<d[ch[x][1]])swap(ch[x][0],ch[x][1]);  
    d[x]=d[ch[x][1]]+1;  
    return x;  
}  
int gf(int t){
	while(f[t])t=f[t];
	return t;
}
int pop(int x){
	int l=ch[x][0],r=ch[x][1];
	a[x]/=2;
	f[ch[x][0]]=0;f[ch[x][1]]=0;
	ch[x][0]=0;ch[x][1]=0;
	d[x]=0; 
	return merge(l,r);
}
int main(){
	int n,m,i,t,k,x,y,x1,y1;
	while(~scanf("%d",&n)){
		memset(f,0,sizeof(f));
		memset(d,0,sizeof(d));
		memset(ch,0,sizeof(ch));
		d[0]=-1;
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d%d",&t,&k);
		x=gf(t);y=gf(k);
		if(x==y){
			printf("-1\n");
			continue;
		}
		x1=pop(x);y1=pop(y);
		x=merge(x1,x);y=merge(y1,y);
		printf("%d\n",a[merge(x,y)]);
	}
}
}

【洛谷P1456】Monkey King

题目描述Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in i...
  • w_yqts
  • w_yqts
  • 2017-04-20 21:14:34
  • 159

洛谷P1456 Monkey King

题目描述 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in...
  • yangrui2002
  • yangrui2002
  • 2018-01-01 08:44:05
  • 65

洛谷 P1456 Monkey King 题解

标签:左偏树&并查集 思路来源: Q1:为什么用并查集? A1:因为要判断两队猴子是否认识,在此处使用并查集可以便于维护两队猴子间的关系。 Q2:为什么用堆? A2:因为每次对战都需要查找当...
  • fz_zsl
  • fz_zsl
  • 2017-12-02 10:59:51
  • 51

洛谷P1015回文数C++解

  • 2018年03月20日 16:19
  • 673B
  • 下载

洛谷P1456 (HDU 1512)Monkey King

左偏树
  • a1799342217
  • a1799342217
  • 2017-09-26 21:48:05
  • 162

论如何获取洛谷所有用户的ID

前段时间改名的时候问了一些kkk可不可以在某些地方看到洛谷全部人的ID然后kkk说没有这种东西,那这就让我很纳闷了,如果连某个ID是否被使用都不知道那岂不是很尴尬所有我就突发奇想写了一个长得很丑,跑得...
  • Nidhogg__
  • Nidhogg__
  • 2017-05-06 20:25:23
  • 485

[洛谷模板大赛]题解 模板整理QAQ

题目地址:NOIP RP++题目描述不用说,都是交模板就能AC的,昨晚就A了4个题因为时间不太够了…… 最近也想整理模板,看到这么个模板大赛自然是把持不住辣~ 除了T2平衡树不会做之外其他的都可以...
  • Loi_Seavot
  • Loi_Seavot
  • 2015-11-05 09:18:25
  • 861

为什么要用洛谷Online Judge?

1.洛谷有着可爱精简的页面。致力于为OIers/ACMers提供清爽、快捷的编程体验。它不仅仅是一个在线测题系统,更拥有强大的社区、你的提问都能获得及时的解答。洛谷具有在线学习功能,在洛谷自主研发的网...
  • qq_39984146
  • qq_39984146
  • 2018-01-31 12:03:24
  • 141

洛谷上的2道简单的01背包问题

1. P1060 开心的金明(https://www.luogu.org/problem/show?pid=1060)题目描述金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞...
  • RicHaRD_CHen_RCHEN
  • RicHaRD_CHen_RCHEN
  • 2017-08-10 11:32:02
  • 269

洛谷题目按难度点评---入门难度

洛谷题目按难度点评---入门难度 1.p1421 小玉买文具 难度:入门难度 考点:输入,输出,整数的四则运算 适用:小学生 2....
  • mrcrack
  • mrcrack
  • 2017-03-05 11:25:26
  • 1285
收藏助手
不良信息举报
您举报文章:hdu1512&洛谷p1456
举报原因:
原因补充:

(最多只允许输入30个字)