您的位置:首页 > 其它

zoj 3261 Connections in Galaxy War(逆向并查集)

2012-10-05 12:05 363 查看
链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261

题目:

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


Author: MO, Luyi

Source: ZOJ Monthly, November 2009

题目大意:

银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可通过通道找到能量值最大的星球来帮忙。但是有一些通道被怪兽破坏了。

现在先给出原来的所有通道, 然后进行询问,询问有两种方式:

destroy a b: 连接a,b的通道被怪兽破坏了

query a: 询问a能否通过通道找到救兵。

分析与总结:

如果直接按照惯性思维,先用并查集把所有通道连起来,然后在删除边,那么恐怕没什么可行性。

一个很巧妙的方法是,我们可以按照逆向思维,先离线输入所有的输入,然后把被破坏之后的通道连接起来,然后在从最后一个询问往前面推,当有destroy a b时就把a,b再Union起来。

代码:

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

const int HASH = 10000;
const int N = 10005;
const int M = 20005;

map<int, bool>mp;

int f
;
int rank
;
int n,m,Q;
int ans[50005];  // 保存答案

struct Buit{
    int u, v;
}built[M];

struct Query{
    char cmd[10];
    int a, b;
}query[50005];

void make_set(){
    for(int i=0; i<n; ++i) f[i]=i;
}
int find(int x){
    int i,j=x;
    while(j!=f[j]) j=f[j];
    while(x!=j){i=f[x]; f[x]=j; x=i;}
    return j;
}
void Union(int x,int y){
    int a=find(x),b=find(y);;
    if(a==b)return ;
    if(rank[a]>rank) f[b]=a;
    else if(rank[a]<rank[b]) f[a]=b;
    else{
        if(a>b) f[a]=b;
        else f[b]=a;
    } 
}

int main(){
    bool flag=false;
    while(~scanf("%d",&n)){
        for(int i=0; i<n; ++i)
            scanf("%d",&rank[i]);
        scanf("%d",&m);
        for(int i=0; i<m; ++i){
            scanf("%d%d",&built[i].u, &built[i].v);
            if(built[i].u>built[i].v){
                swap(built[i].u, built[i].v);
            }
        }
        scanf("%d",&Q);
        mp.clear();
        for(int i=0; i<Q; ++i){
            scanf("%s",query[i].cmd);
            if(query[i].cmd[0]=='q') scanf("%d",&query[i].a);
            else{
                scanf("%d%d",&query[i].a,&query[i].b);
                if(query[i].a>query[i].b) swap(query[i].a,query[i].b);
                mp[query[i].a*HASH+query[i].b]=true;;
            }
        }
        // 合并没有删除的边
        make_set();
        for(int i=0; i<m; ++i)if(!mp[built[i].u*HASH+built[i].v]){
            Union(built[i].u, built[i].v);
        }
        // 询问
        int k=0;
        for(int i=Q-1; i>=0; --i){
            if(query[i].cmd[0]=='q'){
                int fa=find(query[i].a);
                if(rank[fa]>rank[query[i].a]) ans[k++]=fa;
                else ans[k++]=-1;
            }
            else{
                Union(query[i].a,query[i].b);
            }
        }
        if(flag)puts("");
        else flag=true;
        for(int i=k-1; i>=0; --i)
            printf("%d\n", ans[i]);
    }
    return 0;
}




[b]——  生命的意义,在于赋予它意义。               原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: