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

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 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

2
10 20
1
0 1
5
query 0
query 1
destroy 0 1
query 0
query 1

Sample Output

1
-1
-1
-1

解题思路:简单的并查集,另加一些限制条件:帮助者的能量要大于寻求者,并且是最大,再有帮助者能量不止一个,就寻求编号小的,记得无向边双向标记,反向离线处理答案即可。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn=10005;
 5 const int maxq=50005;
 6 int n,m,q,a,b,f=0,fa[maxn],eng[maxn],ans[maxq],tmp[maxq];
 7 char str[10];bool flag[maxq];
 8 map<pair<int,int>,bool> mp1;
 9 map<int,pair<int,int> > mp2,mp3;
10 void init(){
11     for(int i=0;i<n;++i)fa[i]=i;
12     memset(ans,0,sizeof(ans));
13     memset(flag,false,sizeof(flag));
14     memset(tmp,0,sizeof(tmp));
15     mp1.clear(),mp2.clear(),mp3.clear();
16 }
17 int find_fa(int x){
18     return fa[x]==x?x:fa[x]=find_fa(fa[x]);
19 }
20 void unite(int x,int y){
21     x=find_fa(x),y=find_fa(y);
22     if(x!=y){
23         if(eng[x]>eng[y])fa[y]=x;
24         else if(eng[x]<eng[y])fa[x]=y;
25         else if(x<y)fa[y]=x;
26         else fa[x]=y;
27     }
28 }
29 inline int read(){
30     int x=0,f=1;char ch=getchar();
31     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
32     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
33     return x*f;
34 }
35 inline void print(int x){
36     if(x<0)putchar(‘-‘),x=-x;
37     if(x>9)print(x/10);
38     putchar(x%10+‘0‘);
39 }
40 int main(){
41     while(~scanf("%d",&n)){
42         init();
43         if(f++)puts("");
44         for(int i=0;i<n;++i)eng[i]=read();
45         m=read();
46         for(int i=0;i<m;++i){
47             a=read(),b=read();
48             if(a>b)swap(a,b);///规定方向避免重复,相当于建双向边
49             mp2[i]=make_pair(a,b);
50         }
51         q=read();
52         for(int i=0;i<q;++i){
53             scanf("%s",str);
54             if(!strcmp(str,"query"))tmp[i]=read();///查询
55             else{
56                 a=read(),b=read();flag[i]=true;
57                 if(a>b)swap(a,b);
58                 mp1[mp3[i]=make_pair(a,b)]=true;
59             }
60         }
61         for(int i=0;i<m;++i)
62             if(!mp1[mp2[i]])unite(mp2[i].first,mp2[i].second);
63         for(int i=q-1;i>=0;--i){///反向离线处理答案
64             if(flag[i])unite(mp3[i].first,mp3[i].second);
65             else{
66                 int xx=find_fa(tmp[i]);
67                 ans[i]=eng[xx]>eng[tmp[i]]?xx:-1;///祖先的能量严格大于其本身,同时包含不能得到帮助的情况
68             }
69         }
70         for(int i=0;i<q;++i)
71             if(!flag[i])print(ans[i]),puts("");
72     }
73     return 0;
74 }

原文地址:https://www.cnblogs.com/acgoto/p/10080372.html

时间: 2024-10-06 12:39:00

题解报告:zoj 3261 Connections in Galaxy War(离线并查集)的相关文章

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

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 m

zoj 3261 Connections in Galaxy War(并查集+离线逆向操作)

 题目:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) destory a,b 表示删除连接a,b的边 思路并查集,但是要逆向处理,所以先离线读入,从后向前处理,于是对于destroy操作,等价于连接两个点的操作,然后对于每个询问输出即可 #include<cstdio> #include<cstring> #include<cmath> #i

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

https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可通过通道找到能量值最大的星球来帮忙.但是有一些通道被怪兽破坏了. 现在先给出原来的所有通道, 然后进行询问,询问有两种方式: destroy a b: 连接a,b的通道被怪兽破坏了 query a: 询问a能否通过通道找到救兵,只能找能量值比自己大的救兵. 分析 逆向思维,先离线存储所有的输入操作,

ZOJ 3261 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

Connections in Galaxy War(逆向并查集)

Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 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

ZOJ 3261 Connections in Galaxy War

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意: 一. 给定N个星球(0,...,N-1)及其对应的power(p0,...pn-1) 二. 给定M条星球a与b间(a!=b)的通路 三. 给出Q个指令,"destroy a b"代表此时星球a b间的通路(必定已连接)被破坏了,"query a"要求输出当时应对星球a进行救援的星球的编号 规则为: ①与星球a有直接或间接通路的星

Connections in Galaxy War——逆向并查集

题目链接 题意: 首先给定一个n 然后给你 n个数 表示 每个恒星的能量大小 p[i] 然后给定一个m 然后输入m 条恒星之间的连接关系 再给定一个q  q行 如果 query x 表示查询与 x 号恒星连接并且能量最多的恒星编号 如果 destroy x y 表示破环 x恒星与y恒星 之间的连接 题解: 传统的做法是先把输入的点加入并查集建立关系,然后开始判断询问,遇到destroy就将那两个点之间的关系断开,但是很明显,这样很难做到将其连接关系断开 正难则反 我们先把未被destroy的恒星

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

题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断连接两点的边,query为查询某星球能不能向它人呼叫支援. 还是需要离线逆向并查集求解.思路和HDU 4496很相似,但是此处不一定是把所有边都删去,所以需要删边的情况建立出最终的状态.因为N可以到1e4,所以可以用map嵌套map的方式记录过程中被删去的边,最后再根据删除情况建立状态. 在合并时需

CSUOJ 1601 War (离线并查集求连通块个数)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 130  Solved: 38 [Submit][Status][Web Board] Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B ar