1. 程式人生 > >POJ-1703-Find them, Catch them(並查集分類)

POJ-1703-Find them, Catch them(並查集分類)

Description

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)

Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:

1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.

2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.
Output

For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."
Sample Input

1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
Sample Output

Not sure yet.
In different gangs.
In the same gang.


這個題值得好好做一下。有兩種思路。

思路一:建立虛擬節點。

a表示幫派A,a+N表示幫派B。D[a][b]時,a和b+N合併,b和a+N合併。如果a和b或a+N和b+N為一個幫派則a和b是一個幫派,如果a和b+N或b和a+N為一個幫派則,a和b不是一個幫派。

思路二:帶權並查集。

建一個權陣列,表示與父節點的關係(0同1異)。

find():

每次更新pre[x]時也隨著更新權r[x]。

舉個例子,新節點併入星狀集有三種情況:

1.某節點P0併入根節點r[]=0,那麼r[P0]=1,

2.某節點P1併入一個r[]==1的節點(非根),那麼r[P1]=0;

3.某節點P2併入一個r[]==0的節點(非根),那麼r[P2]=1;

 

 

注意:這個不是真正的模型,只是說明這三種情況merge()之後的權。實際模型在壓縮完成後權值才真正定型。也就說merge()完後該圖應該還是一個星狀結構,P1、P2父節點為根節點。

當然,由於根節點併入其他節點等許多複雜情況,並查集結構不可能一直是星狀的。但原理是一樣的。它和它的爺爺節點的同異,要看它的父節點和它本身的權值。

新的權更新公式:

r[x]=(r[x]+r[t])%2;  //這是更新之後的新權值(和爺爺(新父親)的同異)。

 

merge():

兩個樹合併根節點更新;兩個根節點的關係:

r[fx]=(r[x]+r[y]+1)%2;

 

最後find()之後就成星狀集了,只需要看和根節點是否是同一幫派就OK了,也就是權為0是一類權為1是一類。

 


 

虛擬節點:

#include<cstdio>
using namespace std;
int pre[200010];
int N,M;

int find(int x){
    while(pre[x]!=x){
        int r=pre[x];
        pre[x]=pre[r];
        x=r;
    }
    return x;
} 

void merge(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy) pre[fx]=fy;
}

int same(int x,int y){
    return find(x)==find(y);
}

int main(){
    int T,a,b;
    char c[3];
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N*2;i++)
            pre[i]=i;
        while(M--){
            scanf("%s%d%d",c,&a,&b);
            if(c[0]=='D'){
                merge(a,b+N);
                merge(a+N,b);
            }
            else{
                if(same(a,b)||same(a+N,b+N)) printf("In the same gang.\n");
                else if(same(a,b+N)||same(a+N,b)) printf("In different gangs.\n");
                else printf("Not sure yet.\n");                    
            } 
        }
    }        
    return 0;
}

 

帶權並查集:

#include<cstdio>
using namespace std;
int N,M;
int pre[100005];
int r[100005];//權陣列,代表與父節點的關係 0同 1異
 
int find(int x){    
    if(x==pre[x]) return x;
    int t=pre[x];// 拿到x的父親節點 
    pre[x]=find(pre[x]);
    r[x]=(r[x]+r[t])%2; // 或 r[x]=r[x]^r[t];
    return pre[x];
}

void merge(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy) pre[fx]=fy;
    r[fx]=(r[x]+r[y]+1)%2; // 或 r[fx]=~(r[x]^r[y]); 根節點
}

int main(){
    int T,a,b;
    char c[5];
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N;i++){        
            pre[i]=i;
            r[i]=0;
        }
        while(M--){
            scanf("%s%d%d",c,&a,&b);
            
            if(c[0]=='D'){            
                merge(a,b);
            }
            else {
                if(find(a)==find(b)){
                    if(r[a]!=r[b]) printf("In different gangs.\n");
                    else printf("In the same gang.\n");
                } 
                else printf("Not sure yet.\n");
            }
        }
    }    
    return 0;
}