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

题意:

宇宙发生战争了,需要星球之间支援, 但是邪恶的外星人切断了星球之间的联系。

通过查询是否能获得支援。

思路:

可以通过先储存他们的查询结果,然后将查询没有提及到的边先建立并查集。

然后将查询结果倒着遍历, 这时候再建立并查集, 然后将结果储存在一个数组中。

虽然思路很简单, 但是我写出来还是不对啊。

无奈之下, 只好看kuangbin大佬写的代码, 发现大佬写的代码就是不一样, 他是用map数组存的边的值。

即,map[u][v]=i.表示u-v这条边,而通过这个映射i与used[i]建立联系, 然后通过used[i]决定是否立即建立并查集。

不得不说这个方法太巧妙了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
const int maxn=10005;
int n;
int a[maxn]; //并查集数组
int h[maxn]; //储存战斗力
map<int,int>mapp[maxn];//映射
int used[maxn*2];//判断是否一开始并查集
struct pre
{
    int no;
    int hh;
};
struct data
{
    int x,y;
};
data d[maxn*2]; //储存两点之间的关系
pre re[maxn];  //储存要求得的编号和最大高度
struct que
{
    int op;
    int x,y;
};
que q[maxn*5];
int ans[maxn*5];
void init(int x)
{
    memset (used,0,sizeof(used));
    for (int i=0;i<x;i++)
    {
        a[i]=i;
        re[i].no=i;
        re[i].hh=h[i];
        mapp[i].clear();
    }
}
int finds(int x)
{
    if(x==a[x])
        return x;
    a[x]=finds(a[x]);
    return a[x];
}
void unite (int x,int y)
{
    int temp1=finds(x);
    int temp2=finds(y);
    if(temp1!=temp2)
    {
        a[temp1]=temp2;
        if(re[temp2].hh<re[temp1].hh)
        {
            re[temp2].hh=re[temp1].hh;
            re[temp2].no=re[temp1].no;
        }
        else if(re[temp1].hh==re[temp2].hh&&re[temp1].no<re[temp2].no)
            re[temp2].no=re[temp1].no;
    }
}
int main()
{
    int first=1;
    while (scanf("%d",&n)==1)
    {
        if(first)
            first=0;
        else
            printf("\n");
        for (int i=0;i<n;i++)
            scanf("%d",&h[i]);
        init(n);
        //边数
        int m;
        scanf("%d",&m);
        for (int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(x>y)
                swap(x,y);
            d[i].x=x;
            d[i].y=y;
            mapp[x][y]=i;
        }
        //查询次数
        int ci;
        char s[15];
        scanf("%d",&ci);
        for (int i=0;i<ci;i++)
        {
            scanf("%s",s);
            if(s[0]=='q')
            {
                scanf("%d",&q[i].x);
                q[i].op=0;
            }
            else
            {
                q[i].op=1;
                int x,y;
                scanf("%d%d",&x,&y);
                if(x>y)
                    swap(x,y);
                q[i].x=x;
                q[i].y=y;
                int temp=mapp[x][y];
                used[temp]=1;
            }
        }
        for (int i=0;i<m;i++)
        {
            if(used[i])
                continue;
            else
                unite(d[i].x,d[i].y);
        }
        int cnt=0;
        for (int i=ci-1;i>=0;i--)
        {
            if(!q[i].op)
            {
                int temp1=finds(q[i].x);
                if(h[q[i].x]<re[temp1].hh)
                    ans[cnt++]=re[temp1].no;
                else
                    ans[cnt++]=-1;
            }
            else
            {
                unite(q[i].x,q[i].y);
            }
        }
        for (int i=cnt-1;i>=0;i--)
            printf("%d\n",ans[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/81588870