Connections in Galaxy War

Time Limit: 3 Seconds      Memory Limit: 32768 KB

题目链接http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261

Description:

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 integer pi. When the star A wanted to seek help, it would send the message to the star with the largest power which was connected with star A directly or indirectly. In addition, this star should be more powerful than the star A. 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 star A 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 contains N integers p0, p1, ... , pn-1 (0 <= pi <= 1000000000), representing the power of the i-th star. Then the third line is a single integer M (0 <= M <= 20000), that is the number of tunnels built before the war. Then M lines follows. Each line has two integers a, b (0 <= a, b <= N - 1, a != b), which means star a 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 following Q lines, each line will be written in one of next two formats.

"destroy a b" - the connection between star a and star b was destroyed by the monsters. It's guaranteed that the connection between star a 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

2

10 20

1

0 1

5

query 0

query 1

destroy 0 1

query 0

query 1

Sample Output

1

-1

-1

-1

题意:

先进行合并操作,然后进行询问和分裂操作,询问操作返回相连值最大的那个点的下标,如果值最大的点有多个,则要下标最小;分裂操作就从a,b中间断开。

题解:

因为并查集只能“并”,不能“裂”,所以直接处理不好处理。

但是如果我们反过来看,每次分裂操作都是一次合并操作,刚好可以用并查集处理。

之后,我们维护下集合中最大的值以及最小坐标就行了。

代码如下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <map>
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9. const int N = ,Q = ;
  10. int n,m,que;
  11. char s[Q][];
  12. ll P[N];
  13. struct father{
  14. int fa,id;
  15. ll p;
  16. }f[N];
  17. struct query{
  18. int x,y;
  19. }q[Q];
  20. struct link{
  21. int p1,p2;
  22. }l[N<<];
  23. //用很多数组来储存...
  24. int find(int x){
  25. return f[x].fa==x ? x : f[x].fa=find(f[x].fa);
  26. }
  27. void Union(int x,int y){
  28. int fx=find(x),fy=find(y);
  29. f[fx].fa=fy;
  30. if(f[fx].p>f[fy].p){
  31. f[fy].p=f[fx].p;
  32. f[fy].id=f[fx].id;
  33. }else if(f[fx].p==f[fy].p){
  34. f[fy].id=min(f[fy].id,f[fx].id);
  35. }
  36. }
  37. int main(){
  38. bool flag = false;
  39. while(~scanf("%d",&n)){
  40. map <int ,map<int,int> > mp;
  41. for(int i=;i<=n;i++) f[i].fa=i,f[i].id=i;
  42. for(int i=;i<n;i++) scanf("%lld",&f[i].p),P[i]=f[i].p;
  43. scanf("%d",&m);
  44. for(int i=,tmp1,tmp2;i<=m;i++){
  45. scanf("%d%d",&tmp1,&tmp2);
  46. if(tmp1>tmp2) swap(tmp1,tmp2);
  47. l[i].p1=tmp1;l[i].p2=tmp2;
  48. }
  49. scanf("%d",&que);
  50. for(int i=,tmp1,tmp2;i<=que;i++){
  51. scanf("%s",s[i]);
  52. if(s[i][]=='d'){
  53. scanf("%d%d",&tmp1,&tmp2);
  54. if(tmp1>tmp2) swap(tmp1,tmp2);
  55. q[i].x=tmp1;q[i].y=tmp2;
  56. mp[tmp1][tmp2]=;
  57. }else scanf("%d",&q[i].x);
  58. }
  59. for(int i=;i<=m;i++){
  60. int p1=l[i].p1,p2=l[i].p2;
  61. if(mp[p1][p2]) continue;
  62. int fx=find(p1),fy=find(p2);
  63. if(fx==fy) continue;
  64. Union(p1,p2);
  65. }
  66. int ans[Q];int tot=;
  67. for(int i=que;i>=;i--){
  68. if(s[i][]=='d') Union(q[i].x,q[i].y);
  69. else{
  70. int now = q[i].x;
  71. int fx=find(now);
  72. if(f[fx].p<=P[now] ) ans[++tot]=-;
  73. else ans[++tot]=f[fx].id;
  74. }
  75. }
  76. if(flag) printf("\n");
  77. else flag=true ;
  78. for(int i=tot;i>=;i--) printf("%d\n",ans[i]);
  79. }
  80. return ;
  81. }

ZOJ3261:Connections in Galaxy War(逆向并查集)的更多相关文章

  1. Connections in Galaxy War (逆向并查集)题解

    Connections in Galaxy War In order to strengthen the defense ability, many stars in galaxy allied to ...

  2. Connections in Galaxy War(逆向并查集)

    Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 Time Limit ...

  3. ZOJ 3261 Connections in Galaxy War(逆向并查集)

    参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...

  4. ZOJ3261 Connections in Galaxy War —— 反向并查集

    题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ...

  5. zoj 3261 Connections in Galaxy War(并查集逆向加边)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现 ...

  6. ZOJ 3261 - Connections in Galaxy War ,并查集删边

    In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...

  7. ZOJ - 3261 Connections in Galaxy War(并查集删边)

    https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可 ...

  8. ZOJ 3261 Connections in Galaxy War (逆向+带权并查集)

    题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断 ...

  9. ZOJ-3261 Connections in Galaxy War 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...

随机推荐

  1. stm32+lwip(三):TCP测试

    我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...

  2. 最短路径算法 2.Dijkstra算法

    Dijkstra 算法解决的是带权重的有向图上单源最短路径问题,该算法要求所有边的权重都为非负值.该算法的时间复杂度是O(N2),相比于处理无负权的图时,比Bellmad-Ford算法效率更高. 算法 ...

  3. Java输出日历写法

    package TestString_2; import java.text.ParseException;import java.util.Calendar;import java.util.Gre ...

  4. 年薪20万Python工程师进阶(7):Python资源大全,让你相见恨晚的Python库

    我是 环境管理 管理 Python 版本和环境的工具 pyenv – 简单的 Python 版本管理工具. Vex – 可以在虚拟环境中执行命令. virtualenv – 创建独立 Python 环 ...

  5. OrCAD把原理图中的器件添加到原理图库

    1. 在使用OrCAD的时候,有时需要把别人的原理图里面的器件添加到自己的原理图库,方便以后使用,具体操作如下,依次选择Design Cache---元器件--Copy 2. 选中要存放的原理图库,鼠 ...

  6. 字符串分割(C++)

    一.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串. 参数说明:str为要分解的字符串, ...

  7. cgi、fastcgi、php-cgi、php-fpm的关系

    1. CGI CGI全称是"公共网关接口"(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行"交谈"的一种工具,其 ...

  8. python内置模块[re]

    python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...

  9. Android学习笔记之,调用系统图库,添加自定义字体,屏幕截图

    新年开始的第一天就来学习了慕课迎春活动中的Android心愿分享一课,学到了几个知识点,在此记录一下. 1.调用系统图库调用系统图库用的是intent,步骤为弹出系统图库选择器,选择图片后获取到所选择 ...

  10. Qt QPainter::end: Painter ended whith 2 saced states

    在使用Qt  QPainter 的时候,有时会遇到“QPainter::end: Painter ended whith 2 saced states” 这时由于我们在使用的QPanter.trans ...