HDU 1512 Monkey King(左偏树)
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.
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.
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):
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAXN = 100010; 7 8 int n, m; 9 int fa[MAXN]; 10 11 int key[MAXN], child[MAXN][2], dist[MAXN]; 12 int stk[MAXN], top, node_cnt; 13 int root[MAXN]; 14 15 void init() { 16 dist[0] = -1; 17 top = node_cnt = 0; 18 } 19 20 int newNode(int k) { 21 int x = top ? stk[top--] : ++node_cnt; 22 dist[x] = 1; key[x] = k; 23 child[x][0] = child[x][1] = 0; 24 return x; 25 } 26 27 void maintain(int &x) { 28 if(dist[child[x][0]] < dist[child[x][1]]) 29 swap(child[x][0], child[x][1]); 30 dist[x] = dist[child[x][0]] + 1; 31 } 32 33 int merge(int &x, int &y) { 34 if(x == 0) return y; 35 if(y == 0) return x; 36 if(key[y] > key[x]) swap(x, y); 37 child[x][1] = merge(child[x][1], y); 38 maintain(x); 39 return x; 40 } 41 42 int del(int &x) { 43 if(x != 0) { 44 stk[++top] = x; 45 return merge(child[x][0], child[x][1]); 46 } 47 return 0; 48 } 49 50 int getfather(int x) { 51 return fa[x] == x ? x : fa[x] = getfather(fa[x]); 52 } 53 54 int merge2(int x, int y) { 55 return fa[x] = y; 56 } 57 58 void solve(int u, int v) { 59 int fu = getfather(u); 60 int fv = getfather(v); 61 if(fu == fv) { 62 printf("-1\n"); 63 return ; 64 } 65 int p1 = newNode(key[root[fu]] / 2); 66 int p2 = newNode(key[root[fv]] / 2); 67 int p3 = del(root[fu]); 68 int p4 = del(root[fv]); 69 p3 = merge(p1, p3); 70 p4 = merge(p2, p4); 71 int x = merge2(fu, fv); 72 root[x] = merge(p3, p4); 73 printf("%d\n", key[root[x]]); 74 } 75 76 int main() { 77 int k, u, v; 78 while(scanf("%d", &n) != EOF) { 79 init(); 80 for(int i = 1; i <= n; ++i) { 81 scanf("%d", &k); 82 root[i] = newNode(k); 83 fa[i] = i; 84 } 85 scanf("%d", &m); 86 while(m--) { 87 scanf("%d%d", &u, &v); 88 solve(u, v); 89 } 90 } 91 }
使用pb_ds库。使用方法可以参考WC2015的论文《C++的pb_ds库在OI中的应用》。
下面代码使用的Tag为pairing_heap_tag。
此外,binomial_heap_tag为982MS,rc_binomial_heap_tag直接MLE了。
代码(858MS):
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <ext/pb_ds/priority_queue.hpp> 5 6 using __gnu_pbds::priority_queue; 7 8 const int MAXN = 100010; 9 10 priority_queue<std::pair<int, int>, std::less<std::pair<int, int> >, __gnu_pbds::pairing_heap_tag> root[MAXN]; 11 int fa[MAXN]; 12 int n, m; 13 14 int get_set(int x) { 15 return fa[x] == x ? x : fa[x] = get_set(fa[x]); 16 } 17 18 int solve(int u, int v) { 19 int fu = get_set(u), fv = get_set(v); 20 if(fu == fv) return -1; 21 std::pair<int, int> best_u = root[fu].top(); root[fu].pop(); 22 std::pair<int, int> best_v = root[fv].top(); root[fv].pop(); 23 root[fu].push(std::make_pair(best_u.first / 2, best_u.second)); 24 root[fv].push(std::make_pair(best_v.first / 2, best_v.second)); 25 root[fu].join(root[fv]); fa[fv] = fu; 26 return root[fu].top().first; 27 } 28 29 int main() { 30 int k, u, v; 31 while(scanf("%d", &n) != EOF) { 32 for(int i = 1; i <= n; ++i) { 33 scanf("%d", &k); 34 root[i].clear(); 35 root[i].push(std::make_pair(k, i)); 36 fa[i] = i; 37 } 38 scanf("%d", &m); 39 while(m--) { 40 scanf("%d%d", &u, &v); 41 printf("%d\n", solve(u, v)); 42 } 43 } 44 }