日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

洛谷P1456 Monkey King

發布時間:2025/4/16 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷P1456 Monkey King 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目描述

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

說明

題目可能有多組數據

?

?

可并堆裸題

1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<queue> 6 using namespace std; 7 const int MAXN=200001; 8 #define ls T[x].ch[0] 9 #define rs T[x].ch[1] 10 int read() 11 { 12 int x=0,f=1;char ch=getchar(); 13 while(ch<'0' || ch>'9') {if(ch=='-')f=-1;ch=getchar();} 14 while(ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();} 15 return x*f; 16 } 17 int root,N,All; 18 struct node 19 { 20 int fa,dis,val,ch[2]; 21 }T[MAXN]; 22 int Merge(int x,int y) 23 { 24 if(!x) return y; 25 if(!y) return x; 26 if( T[x].val < T[y].val) swap(x,y); 27 rs=Merge(rs,y); 28 T[rs].fa=x; 29 if(T[ls].dis<T[rs].dis) swap(ls,rs); 30 T[x].dis=T[rs].dis+1; 31 return x; 32 } 33 int Find(int x) 34 { 35 while(T[x].fa) x=T[x].fa; 36 return x; 37 } 38 39 int main() 40 { 41 #ifdef WIN32 42 freopen("a.in","r",stdin); 43 #else 44 #endif 45 T[0].dis=-1; 46 while(scanf("%d",&N)!=EOF) 47 { 48 for(int i=1;i<=N;i++) T[i].val=read(),T[i].ch[0]=T[i].ch[1]=T[i].fa=T[i].dis=0; 49 int M=read(); 50 for(int i=1;i<=M;i++) 51 { 52 int x=read(),y=read(); 53 if(Find(x)==Find(y)) {printf("-1\n");continue;} 54 x=Find(x);y=Find(y); 55 int Max=T[x].val>T[y].val?x:y; 56 T[Max].val/=2; 57 printf("%d\n",T[Max].val); 58 T[ T[Max].ch[0] ].fa=T[ T[Max].ch[1] ].fa = 0; 59 int lson=T[Max].ch[0],rson=T[Max].ch[1]; 60 T[Max].ch[0]=T[Max].ch[1]=0; 61 Merge(Merge(lson,rson),Max); 62 Merge(Find(x),Find(y)); 63 } 64 } 65 }

?

總結

以上是生活随笔為你收集整理的洛谷P1456 Monkey King的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。