【洛谷P1456】Monkey King
来源:互联网 发布:棋牌游戏数据库 编辑:程序博客网 时间:2024/05/22 12:23
题目描述
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
说明
题目可能有多组数据
题解
可并堆裸题,新学左偏树,练练手。
代码
#include<cstring>#include<iostream>#include<cmath>#include<cstdio>int v[100005],fa[100005],dis[100005];int ls[100005],rs[100005];int n,m;inline int read(){ int x=0;char ch=getchar(); while (ch<'0'||ch>'9'){ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x;}using namespace std;int merge(int a,int b){ if (!a) return b; if (!b) return a; if (v[a]<v[b]) swap(a,b); rs[a]=merge(rs[a],b); fa[rs[a]]=a; if (dis[ls[a]]<dis[rs[a]]) swap(ls[a],rs[a]); dis[a]=dis[rs[a]]+1; return a;}int find(int k){ while (fa[k]) k=fa[k]; return k;}int main(){ dis[0]=-1; while (scanf("%d",&n)!=EOF) { for (int i=1;i<=n;i++) { v[i]=read(); fa[i]=0; ls[i]=rs[i]=0; dis[i]=0; } m=read(); while (m--) { int x=read(),y=read(); x=find(x);y=find(y); if (x==y) printf("-1\n");else { int k; if (v[x]>v[y]) k=x;else k=y; v[k]/=2; printf("%d\n",v[k]); fa[ls[k]]=fa[rs[k]]=0; int a=ls[k],b=rs[k]; ls[k]=rs[k]=0; merge(merge(a,b),k); merge(find(x),find(y)); } } } return 0;}
- 【洛谷P1456】Monkey King
- 洛谷 P1456 Monkey King 题解
- 洛谷P1456 (HDU 1512)Monkey King
- hdu 1512 Monkey King
- zju 2334 monkey king
- Monkey King zoj 2334
- zoj2334-Monkey King(超时)
- zoj 2334 Monkey King
- 【zoj2334】【左偏树】Monkey King
- ZOJ 2334 Monkey King
- ZOJ2334-Monkey King【二项堆】
- ZOJ 2334 Monkey King
- ZOJ 2334 Monkey King
- 【HDU 1512】Monkey King
- hdu1512 monkey king 左偏树
- 【ZOJ2334】Monkey King
- hdu 1512 Monkey King
- hdu1512 Monkey King
- Java中private、protected、public和default的区别
- 基本内置类型 声明与定义 static与entern const auto register volatile
- Java 设计模式—单例设计模式总结
- tpshop开源商城敢用吗
- HDU 5792 World is Exploding
- 【洛谷P1456】Monkey King
- Guacamole 介绍以及架构 及安装
- leetocde557: Reverse Words in a String III
- 代码实现添加联系人——Vcard
- Spring+SpringMVC+Mybatis配置整合
- CSS属性大全
- NYOJ-170-网络的可靠性(第三届河南省程序设计大赛C题(思维题))
- hrbust 1794敌兵布阵 树装数组模板题
- 手把手教你如何优化linux服务器