ZOJ 3261-Connections in Galaxy War【反向并查集】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/include_peng/article/details/86571090

题目描述

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个星球,每个星球有一定的power值,两个星球之间可以建立联系,当某个星球寻求帮助时,是向与它有直接或间接联系的星球中且power值最大的星球寻求帮助,如果两个星球之间的联系被destory,则这两个星球将失去直接联系,只可能存在间接联系。

思路

很容易看出这是一道并查集的题,可是与并查集不同的是,这道题不仅要建立联系还要删除联系,如果这样想是很复杂的,但是如果将所有请求保存起来,然后反向请求,离线来做的话,就很简单了,如果遇到query就查询与该星球有联系且power值最大的,如果遇到destory就把两个星球建立联系,最后将答案反向输出。

如果把power值大的作为父亲,则查询起来更加方便了。

代码

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;

const int MaxN = 10005;
const int MaxM = 20005;
const int MaxQ = 50005;
int par[MaxN];
int power[MaxN];
map<pair<int,int>,int> mp;
struct edge{
    int a,b;
    bool flag;
};
edge e[MaxM];

struct query{
    char str[20];
    int a,b;
    int ans;
};
query qu[MaxQ];

void Init()//初始化
{
    for(int i = 0; i < MaxN; i++)
        par[i] = i;
}
int Find(int x)
{
    if(x==par[x])
        return par[x];
    else
        return par[x] = Find(par[x]);
}
void Union(int x, int y)
{
    x = Find(x);
    y = Find(y);
    if(x!=y)
    {
        if(power[x] < power[y] || (power[x] == power[y] && x > y)) swap(x,y);//把power值大的作为父亲,如果power值相同则把序号小的作为父亲
        par[y] = x;
    }
}
int main()
{
    int n,m,q;
    int f = true;
    while(~scanf("%d", &n))
    {
        Init();
        mp.clear();
        for(int i = 0; i < n; i++)
            scanf("%d", &power[i]);
        scanf("%d", &m);
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d", &e[i].a, &e[i].b);
            if(e[i].a>e[i].b)
                swap(e[i].a,e[i].b);
            e[i].flag = true;
            mp[make_pair(e[i].a,e[i].b)] = i;
        }
        scanf("%d", &q);
        for(int i = 0; i < q; i++)
        {
            scanf("%s", qu[i].str);
            if(qu[i].str[0]=='q')
                scanf("%d", &qu[i].a);
            else
            {
                scanf("%d %d", &qu[i].a, &qu[i].b);
                if(qu[i].a>qu[i].b)
                    swap(qu[i].a,qu[i].b);
                int tmp = mp[make_pair(qu[i].a,qu[i].b)];
                e[tmp].flag = false;
            }
        }
        for(int i = 0; i < m; i++)//初始化最后一步时的状态
        {
            if(e[i].flag)
            {
                Union(e[i].a,e[i].b);
            }
        }
        for(int i = q-1; i >= 0; i--)
        {
            if(qu[i].str[0]=='q')
            {
                int tmp = Find(qu[i].a);
                //printf("tmp:%d\n", tmp);
                if(power[tmp]>power[qu[i].a])
                    qu[i].ans = tmp;
                else
                    qu[i].ans = -1;
            }
            else
                Union(qu[i].a,qu[i].b);
        }
        if(f)
            f = false;
        else
            printf("\n");
        for(int i = 0; i < q; i++)
        {
            if(qu[i].str[0]=='q')
                printf("%d\n", qu[i].ans);
        }

    }
    return 0;
}

哈哈~第一篇博文,如有错误请指明~   ฅ●ω●ฅ

猜你喜欢

转载自blog.csdn.net/include_peng/article/details/86571090