1. 程式人生 > >ZOJ3261 Connections in Galaxy War —— 反向並查集

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

esp ges times eth print -a ati ever describe

題目鏈接: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 p0, p1, ... , 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 a, b (0 <= a, b <= 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 —— 反向並查集