hdu1512 Monkey King
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.
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.
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
8
5
5
-1
10
正解:左偏树
左偏树板子题。。每次删除树根,键值除以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;
}
hdu1512 Monkey King的更多相关文章
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
- [HDU1512]Monkey King(左偏树)
用并查集维护猴子们的关系,强壮值用左偏树维护就行了 Code #include <cstdio> #include <algorithm> #include <cstri ...
- hdu1512 Monkey King(并查集,左偏堆)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1512 题目大意:有n个猴子,一开始每个猴子只认识自己.每个猴子有一个力量值,力量值越大表示这个猴子打架 ...
- ZOJ 2334 Monkey King
并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子 Monkey King ...
- 数据结构(左偏树):HDU 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- P1456 Monkey King
题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- Monkey King(左偏树 可并堆)
我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...
- 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- 剑指offer_(17)
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) public class Solution17 { /*思路:参考剑指offer 1.首先设置标志 ...
- 列表总结Canvas和SVG的区别
参考链接: 菜鸟教程 HTML5 内联SVG 经典面试题(讨论canvas与svg的区别) Canvas | SVG ---|--- 通过 JavaScript 来绘制 2D 图形|是一种使用 XML ...
- XShell连接本地Ubuntu虚拟机
VMware Workstation 安装好本地虚拟机之后,直接在虚拟机上敲命令着实不方便. 这个时候我们就需要一个远程命令工具来管理虚拟机,这里推荐使用XShell远程命令行工具 1.下载工具 直接 ...
- WinFrom中使用WPF的窗体
步骤 1.添加WindowsFormsIntegration.dll .System.Windows.Forms.和System.Xaml,PresentationCore.PresentationF ...
- Python 使用心得之--变量命名
目前开发命名规范基本都遵循驼峰命名法,如:userName.我也不将这个规范的特性了,大家都明白.接下来进入正题,如何在python中取一个好名字,让你能更好的见名知意呢? 1.元组(Tuple)类型 ...
- ArrayList去除重复元素(包括字符串和自定义对象)
1.去除重复字符串 package com.online.msym; import java.util.ArrayList; import java.util.Iterator; @SuppressW ...
- efwplusUI框架,支持在Liunx服务器运行的Web开发框架,C#开发
efwplusUI开发框架 这是一个简单Web框架,前台采用amaze UI +requireJS+handlebarsJS,后台采用WebApi+MongoDB,用Nginx做Http服务器,随着. ...
- 前端学PHP之Smarty模板引擎
前面的话 对PHP来说,有很多模板引擎可供选择,但Smarty是一个使用PHP编写出来的,是业界最著名.功能最强大的一种PHP模板引擎.Smarty像PHP一样拥有丰富的函数库,从统计字数到自动缩进. ...
- (转)Nginx + uwsgi + web.py + MySQLdb
一.背景知识: Web服务器上有多种方式运行Python: 传统上的CGI方式:基本已被淘汰 mod_python的模块方式:仍在广泛使用,但生产环境中也被逐步淘汰 uwsgi方式:目前生产环境下最受 ...
- jsonp原生js代码示例
/* mightygumball.js */ /* * get the content of a JSON file using JSONP * update every 3 seconds. * * ...