【ZOJ3261】Connections in Galaxy War(并查集+逆向离线处理)

题目链接

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

【题意】

已知n个顶点的权值,再给出出m条边,以及q次查询,当输入query时查询某个点时,需要输出比它权值大的祖先节点,如果两个节点权值相同,则序号小的为祖先,当输入destroy时删除某两个点之间的关系。

【解题思路】

很容易想到用并查集去维护每个点之间的关系,但是并查集是没有删除的,所以这里用到逆向离线处理,即先记录原先的哪两个点之间有边,进行k次查询时,先不输出答案,如果遇到destroy,则把要删除的那条边的信息记录下来,退出查询后,将没被删除的点之间先用并查集维护,然后逆向遍历k次查询,这样当遇到destroy只要将这两个点加入即可,遇到query时用个数组记录答案,因为是逆向的,千万不要直接输出!

【代码】

#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
const int maxn=5e4+5;
int pre[maxn],a[maxn],destroy[maxn],ans[maxn];
struct Node
{
    int op,x,y;
}node[maxn],p[maxn];
int findroot(int x)
{
    int r=x;
    while(r!=pre[r])
        r=pre[r];
    return r;
}
void add(int x,int y)
{
    int fx=findroot(x);
    int fy=findroot(y);
    if(fx!=fy)
    {
        if(a[fx]!=a[fy])
            a[fx]>a[fy]?pre[fy]=fx:pre[fx]=fy;
        else fx<fy?pre[fy]=fx:pre[fx]=fy;
    }
}
int main()
{
    int n,m,k,x,y,kase=0;
    while(~scanf("%d",&n))
    {
        map<pair<int,int>,int>mp;
        memset(destroy,0,sizeof(destroy));
        if(kase++)printf("\n");
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            pre[i]=i;
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            if(x>y)swap(x,y);
            node[i].x=x;node[i].y=y;
            mp[make_pair(x,y)]=i;
        }
        scanf("%d",&k);
        for(int i=0;i<k;i++)
        {
            char s[10];
            scanf("%s",s);
            if(s[0]=='q')
            {
                scanf("%d",&x);
                p[i].op=0;
                p[i].x=x;
            }
            else
            {
                scanf("%d%d",&x,&y);
                if(x>y)swap(x,y);
                p[i].op=1;
                p[i].x=x;p[i].y=y;
                destroy[mp[make_pair(x,y)]]=1;
            }
        }
        for(int i=0;i<m;i++)
        {
            if(!destroy[i])
                add(node[i].x,node[i].y);
        }
        for(int i=k-1;i>=0;i--)
        {
            if(p[i].op)add(p[i].x,p[i].y);
            else
            {
                int fx=findroot(p[i].x);
                ans[i]=a[fx]>a[p[i].x]?fx:-1;
            }
        }
        for(int i=0;i<k;i++)
        {
            if(!p[i].op)printf("%d\n",ans[i]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/82789448