hdu1512 Monkey King
来源:互联网 发布:狗听得懂人话吗 知乎 编辑:程序博客网 时间:2024/04/30 00:04
Problem Description
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.
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.
Input
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.
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.
Output
For 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.
Sample Input
520161010452 33 43 54 51 5
Sample Output
855-110
正解:左偏树
左偏树板子题。。每次删除树根,键值除以2后再插入树,并且将两棵树合并。
//It is made by wfj_2048~#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <vector>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#define inf 1<<30#define il inline#define RG register#define ll long long#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)using namespace std;struct left_tree{ int fa,ls,rs,dis,key; }ltree[100010];int x,n,m;il int gi(){ RG int x=0,q=0; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;}il int father(RG int x){ if (ltree[x].fa!=x) return father(ltree[x].fa); else return x; }il void build(RG int x,RG int k){ ltree[x]=(left_tree){x,0,0,0,k}; return; }il int merge(RG int x,RG int y){ if (!x) return y; if (!y) return x; if (ltree[x].key<ltree[y].key) swap(x,y); ltree[x].rs=merge(ltree[x].rs,y); RG int &l=ltree[x].ls,&r=ltree[x].rs; ltree[l].fa=ltree[r].fa=x; if (ltree[l].dis<ltree[r].dis) swap(l,r); if (!r) ltree[x].dis=0; else ltree[x].dis=ltree[r].dis+1; return x;}il int del(RG int rt){ RG int l=ltree[rt].ls,r=ltree[rt].rs; ltree[l].fa=l,ltree[r].fa=r; ltree[rt].dis=ltree[rt].ls=ltree[rt].rs=0; return merge(l,r);}il int query(RG int x,RG int y){ RG int l=del(x),r=del(y); ltree[x].key>>=1,ltree[y].key>>=1; l=merge(l,x),r=merge(r,y),l=merge(l,r); return ltree[l].key;}il void work(){ for (RG int i=1;i<=n;++i) x=gi(),build(i,x); m=gi(); for (RG int i=1;i<=m;++i){RG int u=gi(),v=gi(),a=father(u),b=father(v);if (a==b){ printf("-1\n"); continue; }printf("%d\n",query(a,b)); } return;}int main(){ File("monkeyking"); while (scanf("%d",&n)!=EOF) work(); return 0;}
0 0
- hdu1512 monkey king 左偏树
- hdu1512 Monkey King
- hdu1512 Monkey King
- hdu1512 Monkey King
- hdu1512 Monkey King
- [HDU1512] Monkey King
- [HDU1512]Monkey King
- HDU1512 Monkey King 左偏树
- ZOJ2334 HDU1512 Monkey King,左偏树
- hdu1512:Monkey King(左偏树)
- 【HDU1512】【左偏树】Monkey King 题解
- 【左偏树+并查集】Monkey King HDU1512
- hdu1512-Monkey King- 左偏树+并查集
- 左偏树+并查集 hdu1512 Monkey King
- hdu1512 Monkey King【左偏堆、并查集】
- HDU1512 Monkey King【并查集+左偏树】
- [HDU1512]Monkey King(可并堆)
- [HDU1512/ZOJ2334]Monkey King-左偏树-可合并堆
- 【视频】BSR缺陷引起的上行流量问题
- 春节将至,为何我如此“恐归”
- LeetCode 387. First Unique Character in a String
- Codeforces 758B-Blown Garland
- UVA 11992 Fast Matrix Operations
- hdu1512 Monkey King
- 【清明】搜索
- css中absolute使用,用position和transform是div里面的div垂直居中
- Android编程权威指南(第二版)学习笔记(十九)—— 第19章 使用 SoundPool 播放音频
- 剪裁头像裁剪遇到关于小米华为适配问题的解决
- PAT---B1036. 跟奥巴马一起编程(15)
- Windows驱动程序学习步骤
- 单片机学习
- bzoj4197 [Noi2015]寿司晚宴