hdu 1512 Monkey King
来源:互联网 发布:sql注入电子书 编辑:程序博客网 时间:2024/04/30 10:02
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cmath>#include <queue>#include <vector>using namespace std;#define N 100010int n,m;int fa[N];struct Node{ int l,r,dist,value;}t[N];inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();} return x*f;}int Find(int x){ if(x!=fa[x])fa[x]=Find(fa[x]); return fa[x];}int Merge(int a,int b){ if(!a||!b) return a+b; if(t[a].value<t[b].value) swap(a,b); t[a].r=Merge(t[a].r,b); fa[t[a].r]=a; if(t[t[a].l].dist<t[t[a].r].dist) swap(t[a].l,t[a].r); if(t[a].r==0) t[a].dist=0; else t[a].dist=t[t[a].r].dist+1; return a;}int Del_root(int x){ int left=t[x].l,right=t[x].r; fa[left]=left;fa[right]=right; t[x].l=t[x].r=t[x].dist=0; return Merge(left,right);}int main(){ while(scanf("%d",&n)!=EOF) { memset(t,0,sizeof(t)); t[0].dist=-1; for(int i=1;i<=n;i++) { t[i].value=read(); fa[i]=i; } m=read(); for(int i=1;i<=m;i++) { int x=read(),y=read(); int root1=Find(x),root2=Find(y); if(root1==root2) { printf("-1\n"); continue; } int t1=Del_root(root1),t2=Del_root(root2); t[root1].value/=2;t[root2].value/=2; t1=Merge(t1,root1); t2=Merge(t2,root2); printf("%d\n",t[Merge(t1,t2)].value); } } return 0;}
0 0
- hdu 1512 Monkey King
- 【HDU 1512】Monkey King
- hdu 1512 Monkey King
- HDU 1512 Monkey King
- HDU 1512 Monkey King
- HDU-1512-Monkey King
- hdu 1512 Monkey King 左偏树
- hdu 1512Monkey King 左偏树
- HDU 1512 Monkey King 左偏树
- hdu 1512 Monkey King 左偏树
- HDU 1512 Monkey King(左偏堆)
- 【HDU】1512 Monkey King 左偏树
- HDU 1512 Monkey King(左偏树)
- 左偏树 Monkey King HDU-1512
- [hdu-1512]Monkey King 题解
- hdu 1512 Monkey King and ZOJ 2334
- ZOJ 2334 HDU 1512 Monkey King
- hdu 1512 Monkey King(坑)
- 生产环境 JDK6 升级 JDK8
- 8.Masonry的使用方法
- poj 3984迷宫问题(bfs加打印路径)
- ListView适配器和监听器
- pyhon3.4 requests模块模拟post登陆csdn
- hdu 1512 Monkey King
- [BZOJ4300] 绝世好题
- 最新 Sublime Text3 激活码 (Build 3114 有效)
- latex初探
- 《Qt5学习笔记4》QHBoxLayout和QVBoxLayout
- BZOJ 4264: 小C找朋友|Hash
- Java中 ArrayList、Vector和LinkedList
- 文章标题
- Who Am I