HDU 1512 Monkey King(左偏树)
2013-07-30 16:06
483 查看
Description
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.
Input
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.
Output
For 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.
题目大意:一开始有n只孤独的猴子,然后他们要打m次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-1.
思路:需要的操作有,选出最牛叉的猴子,合并两堆猴子,让最牛逼的猴子战力减半,比较合适的数据结构就是左偏树啦~
代码(781MS):
View Code
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.
Input
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.
Output
For 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.
题目大意:一开始有n只孤独的猴子,然后他们要打m次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-1.
思路:需要的操作有,选出最牛叉的猴子,合并两堆猴子,让最牛逼的猴子战力减半,比较合适的数据结构就是左偏树啦~
代码(781MS):
#include <cstdio> #include <cstring> #include <algorithm> #include <ext/pb_ds/priority_queue.hpp> using __gnu_pbds::priority_queue; const int MAXN = 100010; priority_queue<std::pair<int, int>, std::less<std::pair<int, int> >, __gnu_pbds::pairing_heap_tag> root[MAXN]; int fa[MAXN]; int n, m; int get_set(int x) { return fa[x] == x ? x : fa[x] = get_set(fa[x]); } int solve(int u, int v) { int fu = get_set(u), fv = get_set(v); if(fu == fv) return -1; std::pair<int, int> best_u = root[fu].top(); root[fu].pop(); std::pair<int, int> best_v = root[fv].top(); root[fv].pop(); root[fu].push(std::make_pair(best_u.first / 2, best_u.second)); root[fv].push(std::make_pair(best_v.first / 2, best_v.second)); root[fu].join(root[fv]); fa[fv] = fu; return root[fu].top().first; } int main() { int k, u, v; while(scanf("%d", &n) != EOF) { for(int i = 1; i <= n; ++i) { scanf("%d", &k); root[i].clear(); root[i].push(std::make_pair(k, i)); fa[i] = i; } scanf("%d", &m); while(m--) { scanf("%d%d", &u, &v); printf("%d\n", solve(u, v)); } } }
View Code
相关文章推荐
- HDU 1512 HDOJ 1512 Monkey King ( 左偏树 ) ACM 1512 IN HDU
- HDU1512 Monkey King【并查集+左偏树】
- hdu 1512 Monkey King 左偏树
- 【HDU】1512 Monkey King 左偏树
- HDU 1512 Monkey King(左偏树)
- Hdu 1512 Monkey King 左偏树 解题报告
- 数据结构(左偏树):HDU 1512 Monkey King
- 【左偏树】HDU1512-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 1512 Monkey King ——左偏树
- hdu 1512 Monkey King (左偏树 可并堆)
- HDU 1512 Monkey King 并查集+左偏树
- hdu 1512 Monkey King 左偏树(可并堆)
- hdu-1512 Monkey King [并查集+左偏树]
- hdu 1512 Monkey King (可并堆)