ZOJ 3261 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. ThenM 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
Author:  MO, Luyi
Source:  ZOJ Monthly, November 2009
Submit     Status



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;

typedef pair<int,int> pII;

const int maxn=21000;

int n,m,q;
set<pII> sp;

struct NODE
{
    int power,id;
}node[maxn];

struct QUESTION
{
    char cd;
    int a,b,c;
}QS[maxn*5];

int fa[maxn];

void INIT()
{
    for(int i=0;i<n+10;i++) fa[i]=i;
}

int find(int x)
{
    if(x==fa[x]) return x;
    return fa[x]=find(fa[x]);
}

void disjoint(int a,int b)
{
    int A=find(a);
    int B=find(b);
    if(A==B) return ;
    if(node[A].power!=node[B].power)
    {
        if(node[A].power>node[B].power) fa[B]=A;
        else fa[A]=B;
    }
    else
    {
        if(node[A].id<node[B].id) fa[B]=A;
        else fa[A]=B;
    }
}

int main()
{
    bool fst=true;
while(scanf("%d",&n)!=EOF)
{
    if(fst) fst=false; else putchar(10);

    sp.clear();
    for(int i=0;i<n;i++)
    {
        int x;
        scanf("%d",&x);
        node[i].power=x;node[i].id=i;
    }

    scanf("%d",&m);

    for(int i=0;i<m;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        if(a>b) swap(a,b);
        sp.insert(make_pair(a,b));
    }

    scanf("%d",&q);

    for(int i=0;i<q;i++)
    {
        char cmd[20];
        int a,b;
        scanf("%s",cmd);
        if(cmd[0]=='d')
        {
            scanf("%d%d",&a,&b);
            if(a>b) swap(a,b);
            sp.erase(make_pair(a,b));
            QS[i]=(QUESTION){'D',a,b,0};
        }
        else if(cmd[0]=='q')
        {
            scanf("%d",&a);
            QS[i]=(QUESTION){'Q',a,0,0};
        }
    }

    INIT();

    set<pII>::iterator item=sp.begin();
    for(;item!=sp.end();item++)
    {
        int a=item->first,b=item->second;
        disjoint(a,b);
    }

    vector<int> ans;

    for(int i=q-1;i>=0;i--)
    {
        if(QS[i].cd=='Q')
        {
            int x=find(QS[i].a);
            if(node[x].power<=node[QS[i].a].power) ans.push_back(-1);
            else ans.push_back(x);
        }
        else if(QS[i].cd=='D')
        {
            int a=QS[i].a,b=QS[i].b;
            disjoint(a,b);
        }
    }

    for(int i=ans.size()-1;i>=0;i--) printf("%d\n",ans[i]);

}
    return 0;
}




你可能感兴趣的:(ZOJ 3261 Connections in Galaxy War)