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

 

vector循环:

  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     u = find(u);
 34     v = find(v);
 35     if(u>v) swap(u, v);     //把u设置为编号小得到
 36     if(u!=v)
 37     {
 38         if(a[u]>=a[v]) fa[v] = u;   //如果值相等, 则要编号小的
 39         else fa[u] = v;   //a[fu]<a[fv]
 40     }
 41 }
 42 
 43 int main()
 44 {
 45     int kase = 0;
 46     while(scanf("%d", &n)!=EOF)
 47     {
 48         if(kase++) printf("\n");
 49         for(int i = 0; i<n; i++)
 50         {
 51             scanf("%d", &a[i]);
 52             g[i].clear();
 53             fa[i] = i;
 54         }
 55 
 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             g[u].push_back(v);
 63         }
 64 
 65         scanf("%d", &k);
 66         for(int i = 1; i<=k; i++)
 67         {
 68             char s[10];
 69             int u, v;
 70             scanf("%s", s);
 71             if(!strcmp(s, "destroy"))
 72             {
 73                 q[i].op = 0;
 74                 scanf("%d%d", &u, &v);
 75                 if(u>v) swap(u, v);
 76                 q[i].u = u; q[i].v = v;     //记录查询
 77                 for(int j = 0; j<g[u].size(); j++)  //标记为-1, 表示删除
 78                     if(g[u][j]==v){ g[u][j] = -1; break; }
 79             }
 80             else
 81             {
 82                 q[i].op = 1;    //记录查询
 83                 scanf("%d", &q[i].u);
 84             }
 85         }
 86 
 87         for(int i = 0; i<n; i++)
 88         for(int j = 0; j<g[i].size(); j++)
 89             if(g[i][j]!=-1) //如果是有效结点, 则合并
 90                 Union(i, g[i][j]);
 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

 

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     u = find(u);
 36     v = find(v);
 37     if(u>v) swap(u, v);   //把u设置为编号小得到
 38     if(u!=v)
 39     {
 40         if(a[u]>=a[v]) fa[v] = u;   //如果值相等, 则要编号小的
 41         else fa[u] = v;   //a[fu]<a[fv]
 42     }
 43 }
 44 
 45 int main()
 46 {
 47     int kase = 0;
 48     while(scanf("%d", &n)!=EOF)
 49     {
 50         if(kase++) printf("\n");
 51         for(int i = 0; i<n; i++)
 52         {
 53             scanf("%d", &a[i]);
 54             fa[i] = i;
 55             M[i].clear();
 56         }
 57 
 58         memset(destroyed, 0, sizeof(destroyed));
 59         scanf("%d", &m);
 60         for(int i = 1; i<=m; i++)
 61         {
 62             int u, v;
 63             scanf("%d%d", &u, &v);
 64             if(u>v) swap(u, v);
 65             edge[i].u = u;
 66             edge[i].v = v;
 67             M[u][v] = i;    //记录边的编号
 68         }
 69 
 70         scanf("%d", &k);
 71         for(int i = 1; i<=k; i++)
 72         {
 73             char s[10];
 74             int u, v;
 75             scanf("%s", s);
 76             if(!strcmp(s, "destroy"))
 77             {
 78                 q[i].op = 0;
 79                 scanf("%d%d", &u, &v);
 80                 if(u>v) swap(u, v);
 81                 q[i].u = u; q[i].v = v;
 82                 destroyed[M[u][v]] = 1;     //M[u][v]为边的编号,表明这条边被毁坏
 83             }
 84             else
 85             {
 86                 q[i].op = 1;
 87                 scanf("%d", &q[i].u);
 88             }
 89         }
 90 
 91         for(int i = 1; i<=m; i++)
 92             if(!destroyed[i])   //如果这条边没有被毁坏,则合并两端点
 93                 Union(edge[i].u, edge[i].v);
 94 
 95         for(int i = k; i>=1; i--)
 96         {
 97             if(q[i].op==0) Union(q[i].u, q[i].v);
 98             else
 99             {
100                 int f = find(q[i].u);
101                 ans[i] = (a[f]>a[q[i].u]?f:-1);
102             }
103         }
104 
105         for(int i = 1; i<=k; i++)
106             if(q[i].op==1)
107                 printf("%d\n", ans[i]);
108     }
109 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7667284.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值