洛谷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次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识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
说明
题目可能有多组数据
可并堆裸题
1 #include<cstdio>
2 #include<cmath>
3 #include<algorithm>
4 #include<iostream>
5 #include<queue>
6 using namespace std;
7 const int MAXN=200001;
8 #define ls T[x].ch[0]
9 #define rs T[x].ch[1]
10 int read()
11 {
12 int x=0,f=1;char ch=getchar();
13 while(ch<'0' || ch>'9') {if(ch=='-')f=-1;ch=getchar();}
14 while(ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
15 return x*f;
16 }
17 int root,N,All;
18 struct node
19 {
20 int fa,dis,val,ch[2];
21 }T[MAXN];
22 int Merge(int x,int y)
23 {
24 if(!x) return y;
25 if(!y) return x;
26 if( T[x].val < T[y].val) swap(x,y);
27 rs=Merge(rs,y);
28 T[rs].fa=x;
29 if(T[ls].dis<T[rs].dis) swap(ls,rs);
30 T[x].dis=T[rs].dis+1;
31 return x;
32 }
33 int Find(int x)
34 {
35 while(T[x].fa) x=T[x].fa;
36 return x;
37 }
38
39 int main()
40 {
41 #ifdef WIN32
42 freopen("a.in","r",stdin);
43 #else
44 #endif
45 T[0].dis=-1;
46 while(scanf("%d",&N)!=EOF)
47 {
48 for(int i=1;i<=N;i++) T[i].val=read(),T[i].ch[0]=T[i].ch[1]=T[i].fa=T[i].dis=0;
49 int M=read();
50 for(int i=1;i<=M;i++)
51 {
52 int x=read(),y=read();
53 if(Find(x)==Find(y)) {printf("-1n");continue;}
54 x=Find(x);y=Find(y);
55 int Max=T[x].val>T[y].val?x:y;
56 T[Max].val/=2;
57 printf("%dn",T[Max].val);
58 T[ T[Max].ch[0] ].fa=T[ T[Max].ch[1] ].fa = 0;
59 int lson=T[Max].ch[0],rson=T[Max].ch[1];
60 T[Max].ch[0]=T[Max].ch[1]=0;
61 Merge(Merge(lson,rson),Max);
62 Merge(Find(x),Find(y));
63 }
64 }
65 }
- 【自然框架】之鼠标点功能现(二):表单控件的“应用”—— 代码?只写需要的!
- 基于Docker环境中源码部署容器Nginx
- 使用Ansible playbooks快速构建etcd集群
- 使用系统内置script和scriptreplay命令来记录操作记录
- 【机器学习】我在面试机器学习、大数据岗位时遇到的各种问题
- 【机器学习】机器学习编程语言之争狼烟再起,Python称霸?
- TiDB 1.1 Beta Release
- 【Python环境】Python面试题汇总(二)
- 【Python环境】Python性能优化的20条建议
- 【Python环境】Python面试题汇总(一)
- 如何优雅的编写Dockerfile
- 【干货】找不到适合自己的编程书?我自己动手写了一个热门编程书搜索网站(附PDF书单)
- Docker监控方案(TIG)的研究与实践之Influxdb
- Docker监控方案(TIG)的研究与实践之Telegraf
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 教你如何设置宝塔面板 Brotli压缩
- Message: session not created: This version of ChromeDriver only supports Chrome version 83
- 宝塔BT面板 设置开启TLSV1.3
- 02 复杂度分析_pythoner学习数据结构与算法系列
- 修改Mysql数据库的数据存储位置
- 使用Caddy搭建TLS1.3+HTTP2代理
- 一文带你了解Python爬虫(一)——基本原理介绍
- 关于修改window.navigator.webdriver代码失效问题
- 一文带你了解Python爬虫(二)——四种常见基础爬虫方法介绍
- 当 snapshot 失败时发生了什么
- python 文件管理神器os.walk-文件指定日期整理程序
- 项目驱动-两日速成Docker日记
- python os模块 --- 操作系统接口模块
- 自动采集各种美女图片站并下载图和自动搭建图片站
- 修改其他函数的功能的神器——python装饰器