ZOJ-3261(并查集续路径压缩,灵活应用)
来源:互联网 发布:淘宝卖家电话在哪 编辑:程序博客网 时间:2024/06/05 19:47
In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimension. Then many problems were raised when some of the stars wanted to seek help from the others.
In the galaxy, the stars are numbered from 0 to N-1 and their power was marked by a non-negative integerpi. When the star A wanted to seek help, it would send the message to the star with the largest power which was connected with starA directly or indirectly. In addition, this star should be more powerful than the starA. If there were more than one star which had the same largest power, then the one with the smallest serial number was chosen. And therefore, sometimes starA couldn't find such star for help.
Given the information of the war and the queries about some particular stars, for each query, please find out whether this star could seek another star for help and which star should be chosen.
Input
There are no more than 20 cases. Process to the end of file.
For each cases, the first line contains an integer N (1 <= N <= 10000), which is the number of stars. The second line containsN integers p0, p1, ... , pn-1 (0 <=pi <= 1000000000), representing the power of the i-th star. Then the third line is a single integerM (0 <= M <= 20000), that is the number of tunnels built before the war. ThenM lines follows. Each line has two integers a, b (0 <=a, b <= N - 1, a != b), which means stara and star b has a connection tunnel. It's guaranteed that each connection will only be described once.
In the (M + 2)-th line is an integer Q (0 <= Q <= 50000) which is the number of the information and queries. In the followingQ lines, each line will be written in one of next two formats.
"destroy a b" - the connection between star a and starb was destroyed by the monsters. It's guaranteed that the connection between stara and star b was available before the monsters' attack.
"query a" - star a wanted to know which star it should turn to for help
There is a blank line between consecutive cases.
Output
For each query in the input, if there is no star that star a can turn to for help, then output"-1"; otherwise, output the serial number of the chosen star.
Print a blank line between consecutive cases.
Sample Input
210 2010 15query 0query 1destroy 0 1query 0query 1
Sample Output
1-1-1-1
题意:
n个星球具有各自的强度power[],每个星球只能向强度大于自己的星球求助,给出一些连通关系之后,询问分为两类,查询某个星球是否可以求助到或者破坏某两个相连星球连接关系。
题解:
灵活的使用并查集,并查集并不支持删除操作,但是如果我们倒过来看,将破坏边看作是加入边,那就是一道简单的并查集题目了。主要是这个题目实现中有一些技巧,值得一记。
1,合理的应用SLT使得代码简洁。比如应用到map和哈希的思想使得判断某条边是否被摧毁,变得效率高且简单。(之前竟然开出了10000*10000的数组,真是太蠢了。。)
2,在查询过程中,加入路径压缩算法,使得查询变得更高效。还有那个稍微变化的合并算法,优先将power大的和标号小的作为根。
3,在解题时时常要想到倒置的思想,有时正着看很难的问题,倒过来看就豁然开朗。
4,最后,注意下输出格式。(WA了好几次。。)
代码:
#include<iostream>#include<cstring>#include<math.h>#include<stdlib.h>#include<cstring>#include<cstdio>#include<utility>#include<algorithm>#include<map>using namespace std;typedef long long ll;const int Max = 10005;const int mod = 1e9+7;const int Hash = 10000;int pre[Max];int power[Max];//每个节点的强度pair<int ,int> edge[20005];//记录输入的边struct st{ char ope[10]; int first, second;} ask[50005]; //记录询问int ans[50005];//答案map<int, bool> IsDestroy;//将被毁掉的边映射为trueint Find(int x){ int t = x; while(x != pre[x]) x = pre[x]; while(t != pre[t])//路径压缩 { int temp = pre[t]; pre[t] = x; t =temp; } return x;}void Union(int a, int b){ int ra = Find(a); int rb = Find(b); if(power[ra]>power[rb]) pre[rb] = ra; else if(power[rb]>power[ra]) pre[ra] = rb; else if(ra < rb) pre[rb] = ra; else pre[ra] = rb;}void init( ){ for(int i=0; i<Max; i++) pre[i] = i;}int T,n,m;int main( ){ //freopen("input.txt","r",stdin); bool flag = false; while(scanf("%d", &n)!=EOF) { init(); for(int i=0; i<n; i++) scanf("%d", power+i); scanf("%d", &m); int a, b; for(int i=0; i<m; i++) { scanf("%d%d",&a, &b); if(a>b) swap(a, b); edge[i].first = a; edge[i].second = b; } scanf("%d", &T); IsDestroy.clear(); for(int i=0; i<T; i++) { scanf("%s", ask[i].ope); if(ask[i].ope[0]=='q') scanf("%d", &ask[i].first); else { scanf("%d%d",&a, &b); if(a > b) swap(a, b); ask[i].first = a; ask[i].second = b; IsDestroy[a*Hash+b] = true;//标记ab边被摧毁 } } for(int i=0; i<m; i++) if(!IsDestroy[edge[i].first*Hash+edge[i].second]) Union(edge[i].first, edge[i].second); int cnt = 0; for(int i=T-1; i>-1; i--) { a = ask[i].first; b = ask[i].second; int root = Find(a); if(ask[i].ope[0]=='q') { if(power[root] > power[a]) ans[cnt++] = root; else ans[cnt++] = -1; } else Union(a, b); } if(flag) printf("\n"); else flag = true; for(int i=cnt-1; i>-1; i--) printf("%d\n", ans[i]); } return 0;}
- ZOJ-3261(并查集续路径压缩,灵活应用)
- [并查集+路径压缩]zoj 3261:Connections in Galaxy War
- zoj 3544 路径压缩
- 并查集 & 路径压缩
- 【并查集+压缩路径】
- 并查集路径压缩
- 并查集路径压缩
- 并查集路径压缩
- 并查集路径压缩
- 并查集压缩路径
- 并查集 压缩路径
- 并查集路径压缩
- 并查集 ---压缩路径
- 并查集 路径压缩
- 并查集压缩路径
- 并查集压缩路径
- 并查集-路径压缩
- 并查集 路径压缩
- 从硬件获得内存布局--e820
- Scr888 Recommend iBET iPHONE 7 Red Lucky Draw
- java 递归全排列 (数组,容器两种实现)
- (九)OcTree教程一--OcTree原理介绍
- 51nod 1333 无聊的数学家们
- ZOJ-3261(并查集续路径压缩,灵活应用)
- [树的直径] BZOJ 1999 [Noip2007]Core树网的核
- Linux进程IPC浅析[进程间通信概述与管道]
- C++中string erase函数的使用
- 史上最简单侧滑菜单,0耦合,支持任意ViewGroup。一步集成侧滑(删除)菜单,高仿QQ、IOS。~
- Treeset保证元素唯一且排序的代码示例
- c++primer学习笔记-----3.1命名空间的using 声明
- ECSHOP 数据库结构说明 (适用版本v2.7.3)
- 百度软件测试:一面