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

Posted h_z_cong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ3261 Connections in Galaxy War —— 反向并查集相关的知识,希望对你有一定的参考价值。

题目链接:https://vjudge.net/problem/ZOJ-3261

 

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.可以想到用并查集处理。题目还要求毁掉一些边,那么并查集也需要解除一些点的关系。但并查集只能建立关系,不能解除关系(至少是很难实现的)。

2.既然并查集的强项是建立关系,那么我们就不要强人所难,硬要人家实现解除关系的功能,而要充分利用其强项,怎么利用?先把最终状态建立出来。然后再将操作顺序逆过来:从最后一步操作开始执行,直到第一步结束。当遇到毁边的操作(即解除关系),由于我们是逆向操作,所以此时应该建边(即建立联系)。

 

代码如下(未提交,ZOJ上不了):

技术分享
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 #define ms(a,b) memset((a),(b),sizeof((a)))
 13 using namespace std;
 14 typedef long long LL;
 15 const double EPS = 1e-8;
 16 const int INF = 2e9;
 17 const LL LNF = 2e18;
 18 const int MAXN = 1e5+10;
 19 
 20 int n, m, k;
 21 int a[MAXN], fa[MAXN], ans[MAXN];
 22 vector<int> g[MAXN];
 23 
 24 struct node
 25 {
 26     int op, u, v;
 27 }q[MAXN];
 28 
 29 int find(int x){ return fa[x]==x?x:x=find(fa[x]); }
 30 
 31 void Union(int u, int v)
 32 {
 33     int fu = find(u);
 34     int fv = find(v);
 35     if(fu==fv)
 36         return;
 37 
 38     if(a[fu]>=a[fv]) fa[fv] = fu;   //如果值相等, 则要编号小的
 39     else fa[fu] = fv;   //a[fu]<a[fv]
 40 }
 41 
 42 int main()
 43 {
 44     while(scanf("%d", &n)!=EOF)
 45     {
 46         for(int i = 0; i<n; i++)
 47         {
 48             scanf("%d", &a[i]);
 49             g[i].clear();
 50             fa[i] = i;
 51         }
 52 
 53         scanf("%d", &m);
 54         for(int i = 1; i<=m; i++)
 55         {
 56             int u, v;
 57             scanf("%d%d", &u, &v);
 58             if(u>v) swap(u, v);
 59             g[u].push_back(v);
 60         }
 61 
 62         scanf("%d", &k);
 63         for(int i = 1; i<=k; i++)
 64         {
 65             char s[10];
 66             int u, v;
 67             scanf("%s", s);
 68             if(!strcmp(s, "destroy"))
 69             {
 70                 q[i].op = 0;
 71                 scanf("%d%d", &u, &v);
 72                 if(u>v) swap(u, v);
 73                 q[i].u = u; q[i].v = v;
 74                 for(int j = 0; j<g[u].size(); j++)
 75                     if(g[u][j]==v){ g[u][j] = -1; break; }
 76             }
 77             else
 78             {
 79                 q[i].op = 1;
 80                 scanf("%d", &q[i].u);
 81             }
 82         }
 83 
 84         for(int i = 0; i<n; i++)
 85         for(int j = 0; j<g[i].size(); j++)
 86             if(g[i][j]!=-1)
 87                 Union(i, g[i][j]);
 88 
 89         for(int i = k; i>=1; i--)
 90         {
 91             if(q[i].op==0) Union(q[i].u, q[i].v);
 92             else
 93             {
 94                 int f = find(q[i].u);
 95                 ans[i] = (a[f]>a[q[i].u]?f:-1);
 96             }
 97         }
 98 
 99         for(int i = 1; i<=k; i++)
100             if(q[i].op==1)
101                 printf("%d\n", ans[i]);
102     }
103 }
View Code

 

map优化:

技术分享
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 #define ms(a,b) memset((a),(b),sizeof((a)))
 13 using namespace std;
 14 typedef long long LL;
 15 const double EPS = 1e-8;
 16 const int INF = 2e9;
 17 const LL LNF = 2e18;
 18 const int MAXN = 1e5+10;
 19 
 20 int n, m, k;
 21 int a[MAXN], fa[MAXN], ans[MAXN];
 22 
 23 struct node
 24 {
 25     int op, u, v;
 26 };
 27 node q[MAXN], edge[MAXN];
 28 map<int, int>M[MAXN];
 29 int destroyed[MAXN];   //
 30 
 31 int find(int x){ return fa[x]==x?x:x=find(fa[x]); }
 32 
 33 void Union(int u, int v)
 34 {
 35     int fu = find(u);
 36     int fv = find(v);
 37     if(fu==fv)
 38         return;
 39 
 40     if(a[fu]>=a[fv]) fa[fv] = fu;   //如果值相等, 则要编号小的
 41     else fa[fu] = fv;   //a[fu]<a[fv]
 42 }
 43 
 44 int main()
 45 {
 46     while(scanf("%d", &n)!=EOF)
 47     {
 48         for(int i = 0; i<n; i++)
 49         {
 50             scanf("%d", &a[i]);
 51             fa[i] = i;
 52             M[i].clear();
 53         }
 54 
 55         memset(destroyed, 0, sizeof(destroyed));
 56         scanf("%d", &m);
 57         for(int i = 1; i<=m; i++)
 58         {
 59             int u, v;
 60             scanf("%d%d", &u, &v);
 61             if(u>v) swap(u, v);
 62             edge[i].u = u;
 63             edge[i].v = v;
 64             M[u][v] = i;
 65         }
 66 
 67         scanf("%d", &k);
 68         for(int i = 1; i<=k; i++)
 69         {
 70             char s[10];
 71             int u, v;
 72             scanf("%s", s);
 73             if(!strcmp(s, "destroy"))
 74             {
 75                 q[i].op = 0;
 76                 scanf("%d%d", &u, &v);
 77                 if(u>v) swap(u, v);
 78                 q[i].u = u; q[i].v = v;
 79                 destroyed[M[u][v]] = 1;
 80             }
 81             else
 82             {
 83                 q[i].op = 1;
 84                 scanf("%d", &q[i].u);
 85             }
 86         }
 87 
 88         for(int i = 1; i<=m; i++)
 89             if(!destroyed[i])
 90                 Union(edge[i].u, edge[i].v);
 91 
 92         for(int i = k; i>=1; i--)
 93         {
 94             if(q[i].op==0) Union(q[i].u, q[i].v);
 95             else
 96             {
 97                 int f = find(q[i].u);
 98                 ans[i] = (a[f]>a[q[i].u]?f:-1);
 99             }
100         }
101 
102         for(int i = 1; i<=k; i++)
103             if(q[i].op==1)
104                 printf("%d\n", ans[i]);
105     }
106 }
View Code

 

以上是关于ZOJ3261 Connections in Galaxy War —— 反向并查集的主要内容,如果未能解决你的问题,请参考以下文章

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

ZOJ-3261 Connections in Galaxy War---离线操作+逆序并查集

Connections in Galaxy War ZOJ - 3261 离线操作+逆序并查集 并查集删边

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

zoj3261变形并查集

ga:itemQuantity in ga_sessions_YYYMMDD(大查询)