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.
输入输出样例
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
8
5
5
-1
10
说明
题目可能有多组数据
Solution:
可并堆裸题(同类型的题目就不搞博客了:P3377、P2713)。
思路无脑模拟,维护朋友关系的时候弄个并查集,然后随便搞个可并堆实现,而懒人我就选择pbds配对堆啦(pbds大法好!^o^)
代码:
/*Code by 520 -- 8.27*/ #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/priority_queue.hpp> #define il inline #define ll long long #define RE register #define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++) #define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--) using namespace std; using namespace __gnu_pbds; const int N=100005; int n,m,fa[N]; __gnu_pbds::priority_queue<int,less<int>,pairing_heap_tag> q[N]; int gi(){ int a=0;char x=getchar(); while(x<'0'||x>'9')x=getchar(); while(x>='0'&&x<='9')a=(a<<3)+(a<<1)+(x^48),x=getchar(); return a; } int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);} int main(){ while(scanf("%d",&n)!=EOF){ int x,y; For(i,1,n) fa[i]=i,x=gi(),q[i].push(x); m=gi(); while(m--){ x=gi(),y=gi(); x=find(x),y=find(y); if(x==y){printf("-1\n");continue;} fa[x]=y; RE int tx=q[x].top()>>1,ty=q[y].top()>>1; q[x].pop(),q[y].pop(); q[x].push(tx),q[y].push(ty); q[y].join(q[x]); printf("%d\n",q[y].top()); } For(i,1,n) while(!q[i].empty()) q[i].pop(); } return 0; }