ZOJ3261 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 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个星球编号为0—n-1;能量分别为p[i];有m句话,每句话输入a,b表示星球a和星球b可以相通的;

但是由于银河之战,破坏了一些通道

接下来有Q句话:destroy a b代表ab之间的通道被破坏;

        query a代表求a可以向哪个星球求助,并输出编号,如果没有就输出-1;

各个星球只像能量值比自己大的星球求助,而且是尽量找到最大能量值的星球求助。

如果有多个能量值一样的星球可以求助,则找编号小的。

正着做很麻烦,因为查并集中间会进行路径压缩,导致信息缺损

所以用离线的思想,倒过来想

先把所有需要连接的记录一下,再把所有需要摧毁的记录一下,

预先连接之后不会被摧毁的路线,之后倒过来询问,如果query就正常查并集

如果destory就新增上这条边

查并集内的函数略改一下,每一次找到val值最大的那个

#include<bits/stdc++.h>
using namespace std;
int f[50005],ans[50005];
int val[50005];
struct forma
{
    int op,l,r;
}ask[50005];
int getfri(int num){  
    if(num!=f[num]){
        f[num]=getfri(f[num]);
    }
    return f[num];
}
int deal(int a,int b) //找到权值最大的一点
{
    int roota=getfri(a);
    int rootb=getfri(b);
    if(val[roota]>val[rootb])
        f[rootb]=roota;
    else if(val[roota]<val[rootb])
        f[roota]=rootb;
    else //若相等,找到编号小的
    {
        if(roota>rootb)
            f[roota]=rootb;
        else
            f[rootb]=roota;
    }
}
int main()
{
    int starnum;
    set<int>g[50005];
    int now=0;
    while(~scanf("%d",&starnum))
    {
        if(now++)
            puts("");
        memset(val,0,sizeof(val));
        for(int i=0;i<=50000;i++){
            g[i].clear();
            f[i]=i;
        }
        for(int i=0;i<starnum;i++)
            scanf("%d",&val[i]);
        int num;
        scanf("%d",&num);
        while(num--){
            int a,b;
            scanf("%d%d",&a,&b);
            if(a>b){
                int temp=a;
                a=b;
                b=temp;
            }
            g[a].insert(b); //先记录哪些道路被连接
        }
        int qnum;
        scanf("%d",&qnum);
        for(int i=0;i<qnum;i++){
            char test[20];
            scanf("%s",&test);
            if(test[0]=='q'){
                ask[i].op=1; //1表示query 2表示destroy
                int a;
                scanf("%d",&a);
                ask[i].l=a;
            }
            else{
                ask[i].op=2;
                int a,b;
                scanf("%d%d",&a,&b);
                if(a>b){
                    int temp=a;
                    a=b;
                    b=temp;
                }
                ask[i].l=a,ask[i].r=b;
                g[a].erase(g[a].find(b)); //将以后需要摧毁的道路从总连接道路中去除
            }
        }
        for(int i=0;i<=50000;i++){ //将需要被连接且以后不会被摧毁的预先连接
            for(set<int>::iterator it=g[i].begin();it!=g[i].end();it++){
                deal(i,*it);
            }
        }
        for(int i=qnum-1;i>=0;i--){ //逆向离线查询
            if(ask[i].op==1){
                int root=getfri(ask[i].l);
                if(val[root]>val[ask[i].l])
                    ans[i]=root;
                else
                    ans[i]=-1;
            }
            else if(ask[i].op==2){
                ans[i]=-999;
                deal(ask[i].l,ask[i].r);
            }
        }
        for(int i=0;i<qnum;i++){
            if(ans[i]==-999)
                continue;
            printf("%d\n",ans[i]);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值