麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發(fā)設計 > 正文

hdu 1512 左偏樹

2019-11-11 05:05:40
字體:
來源:轉載
供稿:網(wǎng)友

題目:

Monkey King

Time Limit: 10000/5000 MS (java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5571    Accepted Submission(s): 2395PRoblem DescriptionOnce 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. InputThere 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. OutputFor 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

分析:

每一只猴子看做一個結點,建立左偏樹,樹根維護最大值。若兩結點樹根相同,說明兩只猴子是朋友,輸出-1;

否則,兩根節(jié)點值減半,刪除,重新與原樹合并得兩新樹,兩新樹合并后返回根節(jié)點值。

本題僅涉及到左偏樹部分操作。

左偏樹入門:

http://wenku.baidu.com/view/1c18eff9aef8941ea76e051b.html

http://wenku.baidu.com/view/004aa1ee19e8b8f67c1cb9d4.html?from=search

http://sunmoon-template.blogspot.ca/2014_12_01_archive.html

代碼:

#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<ctype.h>    //tower()#include<set>#include<iomanip>// cout<<setprecision(1)<<fixed<<a;#include<vector>   #include<time.h>  #include<assert.h>  //assert#include<cmath>	#include<algorithm>#include<bitset>#include<limits.h>#include<stack>#include<queue>using namespace std;const int maxn=100050;const int inf=INT_MAX-100;struct node{    int val,dis,lt,rt,fa;//fa 父節(jié)點}ltree[maxn];int n,q,t;void maketree(int a,int k){//結點成樹 a結點值為k     ltree[a].val=k;    ltree[a].fa=a;    ltree[a].lt=ltree[a].rt=0;    ltree[a].dis=(a==0)?-1:0;}int find(int x){//非遞歸路徑壓縮,返回所在樹根節(jié)點     int s,k,j;    s=k=x;    while(s!=ltree[s].fa) s=ltree[s].fa;    while(k!=s){        j=ltree[k].fa;        ltree[k].fa=s;        k=j;    }    return s;}int merge(int a,int b){//兩棵樹合并 改變rt,dis,fa     if(a==0) return b;    if(b==0) return a;    if(ltree[a].val<ltree[b].val) swap(a,b);    ltree[a].rt=merge(ltree[a].rt,b);//b接在a最右邊 	int &l=ltree[a].lt,&r=ltree[a].rt;//左右子樹的引用,取別名,不另占內存,值同時修改        ltree[r].fa=a;//ltree[l].fa=    if(ltree[l].dis<ltree[r].dis) swap(l,r);//維持左偏性     if(r==0) ltree[a].dis=0;	else ltree[a].dis=ltree[r].dis+1;    return a;}int solve(int a,int b){	int tmp,ta,tb;	a=find(a);	b=find(b);	if(a==b) return -1;//	ltree[ltree[a].lt].fa=ltree[a].lt;//	ltree[ltree[a].rt].fa=ltree[a].rt;	ltree[a].val>>=1;	tmp=merge(ltree[a].lt,ltree[a].rt);	ltree[a].dis=ltree[a].lt=ltree[a].rt=0;    ta=merge(tmp,a);//	ltree[ltree[b].lt].fa=ltree[b].lt;//	ltree[ltree[b].rt].fa=ltree[b].rt;	ltree[b].val>>=1; 	tmp=merge(ltree[b].lt,ltree[b].rt);	ltree[b].dis=ltree[b].lt=ltree[b].rt=0;    tb=merge(tmp,b);        tmp=merge(ta,tb);    ltree[tmp].fa=ltree[ta].fa=ltree[tb].fa=tmp;    return ltree[tmp].val;}int main(){//795MS	3540K    while(~scanf("%d",&n)){	    for(int i=1;i<=n;++i){	        scanf("%d",&t);	        maketree(i,t);	    }//	    maketree(0,0);	    scanf("%d",&q);	    int c,d;	    for(int i=0;i<q;++i){	        scanf("%d%d",&c,&d);	        printf("%d/n",solve(c,d));	    }    }    return 0;    }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩黄色片在线观看 | 国产午夜精品一区 | 日韩视频中文 | 精品亚洲一区二区三区 | 日韩电影av在线 | 久草手机在线观看视频 | 久久新地址 | 91精品观看91久久久久久国产 | www日韩大片| 污黄视频在线播放 | 91 免费视频| 久久久久久69 | 国产精品久久久久久久久久三级 | 毛片网站网址 | 宅男噜噜噜66国产免费观看 | www.17c亚洲蜜桃 | 羞羞的动漫在线观看 | 羞羞视频免费观看网站 | 久久99亚洲精品久久99果 | 国产成人高清在线观看 | 蜜桃传媒视频麻豆第一区免费观看 | 亚洲国产精品一区二区三区 | 成人一级免费视频 | 久久亚洲精品国产一区 | 国产成人精品免高潮在线观看 | 奶子吧naiziba.cc免费午夜片在线观看 | 一区二区久久精品66国产精品 | 操碰网| 免费一级毛片免费播放 | 激情小说激情图片激情电影 | 国产亚洲精品久久午夜玫瑰园 | 中午字幕无线码一区2020 | 中文字幕一区在线观看视频 | 精品久久久久久久久久中文字幕 | 久久看视频 | 九九热在线观看视频 | 亚洲成人免费影视 | 欧美日韩色片 | av在线大全| 91久久极品少妇韩国 | 精品久久久久久久久亚洲 |