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

总结自己被坑的两个点

1.输入边的时候要注意,应该把边从小到大排序或从大到小排序,不然可能会出现,加边的时候是1,2但是删边的时候是2,1的情况

2.这个应该不算坑点。。。是我自己没考虑到:要把不是将被删除的边先连起来。。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<string>
  6 #include<cstring>
  7 #include<queue>
  8 #include<vector>
  9 #include<map>
 10 #include<cmath>
 11 #include<stack>
 12 using namespace std;
 13 #define maxn 200005
 14
 15 int fa[maxn],a[maxn];
 16 map<int,int>mp[maxn];
 17
 18
 19 void init(int n){
 20     for(int i=0;i<n+50;i++){
 21         fa[i]=i;
 22         a[i]=0;
 23         mp[i].clear();
 24     }
 25 }
 26
 27 int Find(int x){
 28     int r=x,y;
 29     while(x!=fa[x]){
 30         x=fa[x];
 31     }
 32     while(r!=x){
 33         y=fa[r];
 34         fa[r]=x;
 35         r=y;
 36     }
 37     return x;
 38 }
 39
 40 void join(int x,int y){
 41     int xx=Find(x);
 42     int yy=Find(y);
 43     if(xx!=yy){
 44         if(a[xx]==a[yy]){
 45             if(xx<yy) fa[yy]=xx;
 46             else fa[xx]=yy;
 47         }
 48         else{
 49             if(a[xx]>a[yy]) fa[yy]=xx;
 50             else fa[xx]=yy;
 51         }
 52     }
 53 }
 54
 55 struct sair{
 56     char s[15];
 57     int x,y;
 58 }p[maxn];
 59
 60 struct Line{
 61     int x,y;
 62 }L[maxn];
 63
 64 int main(){
 65     int n;
 66     int kong=0;
 67     while(~scanf("%d",&n)){
 68         init(n);
 69
 70         if(kong) printf("\n");
 71         for(int i=0;i<n;i++) scanf("%d",&a[i]);
 72         int m;
 73         scanf("%d",&m);
 74         for(int i=1;i<=m;i++){
 75             scanf("%d %d",&L[i].x,&L[i].y);
 76             if(L[i].x>L[i].y) swap(L[i].x,L[i].y);
 77         }
 78         int k;
 79         scanf("%d%*c",&k);
 80         for(int i=1;i<=k;i++){
 81             scanf("%s",&p[i].s);
 82             if(p[i].s[0]==‘q‘) scanf("%d%*c",&p[i].x);
 83             else{
 84                 scanf("%d %d%*c",&p[i].x,&p[i].y);
 85                 if(p[i].x>p[i].y) swap(p[i].x,p[i].y);
 86                 mp[p[i].x][p[i].y]=1;
 87             }
 88         }
 89         int tmp;
 90         stack<int>st;
 91         for(int i=1;i<=m;i++){///容易忽略
 92             if(!mp[L[i].x][L[i].y]) join(L[i].x,L[i].y);
 93         }
 94         for(int i=k;i>=1;i--){
 95             if(p[i].s[0]==‘q‘){
 96                 tmp=Find(p[i].x);
 97                 if(a[tmp]>a[p[i].x]) st.push(tmp);
 98                 else st.push(-1);
 99             }
100             else{
101                 join(p[i].x,p[i].y);
102             }
103         }
104         while(!st.empty()){
105             printf("%d\n",st.top());
106             st.pop();
107         }
108         kong++;
109     }
110
111
112 }

原文地址:https://www.cnblogs.com/Fighting-sh/p/9946044.html

时间: 2024-10-10 05:02:45

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

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(并查集+离线逆向操作)

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

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(并查集删边)

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

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

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

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

UVA 10158 War(并查集)

War A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your country's espionage by attending the peace-talks taking place these days (incognito, of course). There are n people at the talks (not including y

题解报告: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 dimen

hdu-2874 Connections between cities(lca+tarjan+并查集)

题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, s