1. 程式人生 > >ZOJ3261 Connections in Galaxy War 離線逆向查並集

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]);
        }
    }
}