您的位置:首页 > 其它

ZOJ-3261(并查集续路径压缩,灵活应用)

2017-04-05 21:01 302 查看
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, 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

题意:

                  n个星球具有各自的强度power[],每个星球只能向强度大于自己的星球求助,给出一些连通关系之后,询问分为两类,查询某个星球是否可以求助到或者破坏某两个相连星球连接关系。

题解:

                    灵活的使用并查集,并查集并不支持删除操作,但是如果我们倒过来看,将破坏边看作是加入边,那就是一道简单的并查集题目了。主要是这个题目实现中有一些技巧,值得一记。

                     1,合理的应用SLT使得代码简洁。比如应用到map和哈希的思想使得判断某条边是否被摧毁,变得效率高且简单。(之前竟然开出了10000*10000的数组,真是太蠢了。。)

                     2,在查询过程中,加入路径压缩算法,使得查询变得更高效。还有那个稍微变化的合并算法,优先将power大的和标号小的作为根。

                     3,在解题时时常要想到倒置的思想,有时正着看很难的问题,倒过来看就豁然开朗。

                     4,最后,注意下输出格式。(WA了好几次。。)

代码:

#include<iostream>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<cstring>
#include<cstdio>
#include<utility>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int Max = 10005;
const int mod = 1e9+7;
const int Hash = 10000;
int pre[Max];
int power[Max];//每个节点的强度
pair<int ,int> edge[20005];//记录输入的边
struct st
{
char ope[10];
int first, second;
} ask[50005]; //记录询问
int ans[50005];//答案
map<int, bool> IsDestroy;//将被毁掉的边映射为true
int Find(int x)
{
int t = x;
while(x != pre[x])
x = pre[x];
while(t != pre[t])//路径压缩
{
int temp = pre[t];
pre[t] = x;
t =temp;
}
return x;
}
void Union(int a, int b)
{
int ra = Find(a);
int rb = Find(b);
if(power[ra]>power[rb])
pre[rb] = ra;
else if(power[rb]>power[ra])
pre[ra] = rb;
else if(ra < rb)
pre[rb] = ra;
else
pre[ra] = rb;
}
void init( )
{
for(int i=0; i<Max; i++)
pre[i] = i;
}
int T,n,m;
int main( )
{
//freopen("input.txt","r",stdin);
bool flag = false;
while(scanf("%d", &n)!=EOF)
{
init();
for(int i=0; i<n; i++)
scanf("%d", power+i);
scanf("%d", &m);
int a, b;
for(int i=0; i<m; i++)
{
scanf("%d%d",&a, &b);
if(a>b)
swap(a, b);
edge[i].first = a;
edge[i].second = b;
}
scanf("%d", &T);
IsDestroy.clear();
for(int i=0; i<T; i++)
{
scanf("%s", ask[i].ope);
if(ask[i].ope[0]=='q')
scanf("%d", &ask[i].first);
else
{
scanf("%d%d",&a, &b);
if(a > b)
swap(a, b);
ask[i].first = a;
ask[i].second = b;
IsDestroy[a*Hash+b] = true;//标记ab边被摧毁
}
}
for(int i=0; i<m; i++)
if(!IsDestroy[edge[i].first*Hash+edge[i].second])
Union(edge[i].first, edge[i].second);
int cnt = 0;
for(int i=T-1; i>-1; i--)
{
a = ask[i].first;
b = ask[i].second;
int root = Find(a);
if(ask[i].ope[0]=='q')
{
if(power[root] > power[a])
ans[cnt++] = root;
else
ans[cnt++] = -1;
}
else
Union(a, b);
}
if(flag)
printf("\n");
else
flag = true;
for(int i=cnt-1; i>-1; i--)
printf("%d\n", ans[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  博客 ACM ZOJ 并查集