这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态,这就能套并查集了。中途的询问结果用栈存下来,输出的时候一个个出栈就好

  还有一个共同点,都是科幻风格的,都是星际战争,通信都靠隧道,不知道出题人有没有借鉴的意思

  假装这篇博客是我刚写完星球大战这题时写的(其实是一年零两个月二十多天以后,写了zoj那题以后写的 2019年01月25日16:35:27)

[JSOI2008]星球大战

题目描述

  很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系。

  某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球。这些星球通过特殊的以太隧道互相直接或间接地连接。

  但好景不长,很快帝国又重新造出了他的超级武器。凭借这超级武器的力量,帝国开始有计划地摧毁反抗军占领的星球。由于星球的不断被摧毁,两个星球之间的通讯通道也开始不可靠起来。

  现在,反抗军首领交给你一个任务:给出原来两个星球之间的以太隧道连通情况以及帝国打击的星球顺序,以尽量快的速度求出每一次打击之后反抗军占据的星球的连通块的个数。(如果两个星球可以通过现存的以太通道直接或间接地连通,则这两个星球在同一个连通块中)。

输入输出格式

输入格式:

  输入文件第一行包含两个整数,$N (1<=N<=2M)$和$M (1<=M<=200,000)$,分别表示星球的数目和以太隧道的数目。星球用$ 0 ~ N−1 $的整数编号。

  接下来的 $M$ 行,每行包括两个整数 $X$, $Y$,其中($0<=X<>Y$ 表示星球 $x$ 和星球 $y$ 之间有 “以太” 隧道,可以直接通讯。

  接下来的一行为一个整数 $k$ ,表示将遭受攻击的星球的数目。

  接下来的 $k$ 行,每行有一个整数,按照顺序列出了帝国军的攻击目标。这 $k$ 个数互不相同,且都在 $0$ 到$n−1$的范围内。

输出格式:

  第一行是开始时星球的连通块个数。接下来的 $k$ 行,每行一个整数,表示经过该次打击后现存星球的连通块个数。

输入输出样例

输入样例#1: 复制

  1. 8 13
  2. 0 1
  3. 1 6
  4. 6 5
  5. 5 0
  6. 0 6
  7. 1 2
  8. 2 3
  9. 3 4
  10. 4 5
  11. 7 1
  12. 7 2
  13. 7 6
  14. 3 6
  15. 5
  16. 1
  17. 6
  18. 3
  19. 5
  20. 7
输出样例#1: 复制

  1. 1
  2. 1
  3. 1
  4. 2
  5. 3
  6. 3

说明

[JSOI2008]

源代码

  1. #include<stdio.h>//当年的一些中文注释的编码坏掉了
  2.  
  3. int n,m,k;//n??μ?mì?±?k???Y?ù????
  4.  
  5. int ruin[]={};
  6. bool ru[]={};//ê?·?±??Y?ù
  7. int num=;
  8. int ans[]={},top=;
  9.  
  10. struct Edge{
  11. int next,to;
  12. }e[];
  13. int head[]={},cnt=;//40íò??μ?20íòì??T?ò±?
  14. void add(int u,int v)
  15. {
  16. e[cnt].to=v;
  17. e[cnt].next=head[u];
  18. head[u]=cnt++;
  19. }
  20.  
  21. int fa[];//?ó0?e£?2??ó1?e
  22. int find(int x)
  23. {
  24. return fa[x]==x?x:(fa[x]=find(fa[x]));
  25. }
  26. inline bool uni(int x,int y)
  27. {
  28. x=find(x),y=find(y);
  29. if(x==y) return false;
  30. fa[x]=y;
  31. return true;
  32. }
  33.  
  34. int main()
  35. {
  36. //freopen("test.txt","r",stdin);
  37. for(int i=;i<;i++)
  38. fa[i]=i;
  39.  
  40. scanf("%d%d",&n,&m);
  41. for(int i=,u,v;i<m;i++)
  42. {
  43. scanf("%d%d",&u,&v);
  44. //printf("%d %d %d\n",cnt,u,v);
  45. add(u,v);
  46. //printf("%d %d %d\n\n",cnt,v,u);
  47. add(v,u);
  48. }
  49. scanf("%d",&k);
  50. for(int i=;i<k;i++)
  51. scanf("%d",ruin+i),ru[ruin[i]]=true;
  52.  
  53. num=;
  54. for(int i=;i<n;i++)
  55. {
  56. if(!ru[i])
  57. {
  58. for(int j=head[i];j;j=e[j].next)
  59. {
  60. if(!ru[e[j].to])
  61. uni(i,e[j].to);
  62. }
  63. }
  64. }//í3??è?2??Y?ùoóμ?á?í?·?á?num
  65. for(int i=;i<n;i++)
  66. if(find(i)==i&&!ru[i]) num++;
  67. ans[top++]=num;
  68. //printf("^^^^\n");
  69. for(int i=k-;i>=;i--)
  70. {
  71. ru[ruin[i]]=false;
  72. int u=ruin[i];
  73. int m=/*áùê±í3??áaí?·?á??eê§*/;
  74. for(int j=head[u];j;j=e[j].next)
  75. {
  76. if(!ru[e[j].to])
  77. if(uni(u,e[j].to))
  78. {
  79. m++;
  80. //printf("%d %d ",u,e[j].to);
  81. //printf("%d\n",j);
  82. }
  83. }
  84. num-=m-;
  85. ans[top++]=num;
  86. }
  87. top=k;
  88. while(top>=)
  89. {
  90. printf("%d\n",ans[top]);
  91. top--;
  92. }
  93.  
  94. return ;
  95. }

星球大战

Connections in Galaxy War

Connections in Galaxy War


Time Limit: 3 Seconds      Memory Limit: 32768 KB

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 p0p1, ... , 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 ab (0 <= ab <= 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

  1. 2
  2. 10 20
  3. 1
  4. 0 1
  5. 5
  6. query 0
  7. query 1
  8. destroy 0 1
  9. query 0
  10. query 1

Sample Output

  1. 1
  2. -1
  3. -1
  4. -1

Author: MO, Luyi
Source: ZOJ Monthly, November 2009

源代码

  1. #include<set>
  2. #include<stdio.h>
  3. #include<algorithm>
  4.  
  5. int n,m=-,q;
  6.  
  7. struct Opt{//o==d:a b; o==q: a;
  8. char o;
  9. int a;
  10. int b;
  11. int id;
  12. bool operator < (const Opt & x)const{
  13. return id>x.id;
  14. }
  15. }opt[];
  16.  
  17. struct Edge{
  18. int s;
  19. int t;
  20. bool operator < (const Edge & x)const{
  21. if(s==x.s) return t<x.t;
  22. return s<x.s;
  23. }
  24. };
  25. std::set<Edge> e;
  26. std::set<Edge>::iterator it;
  27. int ans[];
  28.  
  29. int p[],mx[],cnt[];
  30. int fa[];
  31. int find(int x)
  32. {
  33. if(fa[x]==x) return x;
  34. fa[x]=find(fa[x]);
  35. cnt[x]=cnt[fa[x]];
  36. mx[x]=mx[fa[x]];
  37. return fa[x];
  38. }
  39. void uni(int x,int y)
  40. {
  41. x=find(x);
  42. y=find(y);
  43. fa[x]=y;
  44. cnt[y]+=cnt[x];
  45. cnt[x]=cnt[y];
  46. if(p[mx[x]]>p[mx[y]])
  47. {
  48. mx[y]=mx[x];
  49. }
  50. else if(p[mx[x]]==p[mx[y]])
  51. {
  52. mx[y]=mx[x]<mx[y]?mx[x]:mx[y];
  53. }
  54. mx[x]=mx[y];
  55. }
  56.  
  57. int main()
  58. {
  59. freopen("test.in","r",stdin);
  60. while(~scanf("%d",&n))
  61. {
  62. if(m>=) printf("\n");
  63. for(int i=;i<n;i++) mx[i]=fa[i]=i,cnt[i]=;
  64. for(int i=;i<n;i++) scanf("%d",p+i);
  65. scanf("%d",&m);
  66. for(int i=,a,b;i<=m;i++)
  67. {
  68. scanf("%d%d",&a,&b);
  69. e.insert({a,b});//查询时e.find({a,b})!=e.end()||e.find({b,a})!=e.end()
  70. }
  71. scanf("%d",&q);
  72. for(int i=;i<=q;i++)
  73. {
  74. int a,b=;
  75. char str[];
  76. scanf("%s%d",str,&a);
  77. if(str[]=='d')
  78. {
  79. scanf("%d",&b);
  80. it=e.find({a,b});
  81. if(it==e.end()) it=e.find({b,a});
  82. e.erase(it);
  83. }
  84. opt[i]={str[],a,b,i};
  85. }
  86. std::sort(opt+,opt++q);
  87. for(it=e.begin();it!=e.end();it++)
  88. uni(it->s,it->t);
  89. int num=;
  90. for(int i=;i<=q;i++)
  91. {
  92. if(opt[i].o=='d')
  93. {
  94. uni(opt[i].a,opt[i].b);
  95. }
  96. else//q
  97. {
  98. find(opt[i].a);
  99. if(p[mx[opt[i].a]]<=p[opt[i].a]) ans[num++]=-;
  100. else ans[num++]=mx[opt[i].a];
  101. }
  102. }
  103.  
  104. while(num>) printf("%d\n",ans[--num]);
  105.  
  106. e.clear();
  107. }
  108. return ;
  109. }

Connections in Galaxy War

洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)的更多相关文章

  1. zoj 3261 Connections in Galaxy War

    点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...

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

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

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

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

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

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

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

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

  6. 题解报告:zoj 3261 Connections in Galaxy War(离线并查集)

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

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

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

  8. BZOJ 1015: [JSOI2008]星球大战starwar 并查集

    1015: [JSOI2008]星球大战starwar Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝 ...

  9. BZOJ 1015 [JSOI2008]星球大战starwar

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 3551  Solved: 1581[Submit ...

随机推荐

  1. poi读写Excel

    poi读写Excel 对于一个程序员来说,文件操作是经常遇到的,尤其是对Excel文件的操作. 在这里介绍一下我在项目中用到的一个操作Excel的工具——POI.关于POI的一些概念,网络上很多,详细 ...

  2. spring:为javabean的集合对象注入属性值

    spring:为JavaBean的集合对象注入属性值 在 spring 中可以对List.Set.Map 等集合进行配置,不过根据集合类型的不同,需要使用不同的标签配置对应相应的集合. 1.创建 Ts ...

  3. servlet中的中文乱码问题

    老师总会说道:学完这个知识点,我们来谈谈中文乱码问题. 乱码的问题总是无处不在,处理不好会给用户带极差的用户体验. 那么我们来记录一下servlet中的乱码问题吧! 1.服务器向客户端响应时出现的乱码 ...

  4. 【POJ 3076】 Sudoku

    [题目链接] http://poj.org/problem?id=3076 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 [代码] #include <algorit ...

  5. 关于api-ms-win-crt-runtimel1-1-0.dll缺失的解决方案

    关于api-ms-win-crt-runtimel1-1-0.dll缺失的解决方案 目录 关于api-ms-win-crt-runtimel1-1-0dll缺失的解决方案 目录 安装VC redite ...

  6. Java-java-com-util-common-service:ServiceException.java

    ylbtech-Java-java-com-util-common-service:ServiceException.java 1.返回顶部 1. package com.shineyoo.manag ...

  7. Java常用类及反射,类加载

    1.系统相关类 Java提供了System类和Runtime类来与程序运行的平台进行交互 A.System类代表当前Java程序的运行平台 a. System类是一个final类,该类的所有属性和方法 ...

  8. go之切片

    一.概念 关于切片 1.切片是对数组一个连续片段的引用,所以切片是一个引用类型 2.切片是数组一样可以索引,可以通过len函数获取切片的数据长度.(数组也可以通过len获取) 3.切片是一个长度可变的 ...

  9. (三)Appium-desktop 打包

    appium-desktop经过二次开发后,需要打包为应用提供给其它同学使用.我们知道appium-desktop是使用electron来构建跨平台桌面应用程序.electron有electron-p ...

  10. pgsql 远程机器无法连接数据库报错处理方法

    因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本地localhost访问正常,在相同网段的远程机器访问报如下错误 “server closed the connection unexpe ...