zoukankan      html  css  js  c++  java
  • poj 1703 Find them, Catch them (并查集)

    Find them, Catch them
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 28649   Accepted: 8761

    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.
    

    Source

    给出两种方法,一种是设立对立集,即a的对立集为a+N,然后对立集再与对立元素合并,最后判断时find相同的就是同gang,找到一个与其对立元素的对立集相同的为不同gang,其他情况为not sure。代码:

     1 //Accepted    900K    329MS    C++    961B    2014-03-28 16:37:52
     2 #include<stdio.h>
     3 #define N 100000
     4 int set[2*N+1];
     5 int find(int x)
     6 {
     7     if(x!=set[x])
     8         set[x]=find(set[x]);
     9     return set[x];
    10 } 
    11 void merge(int a,int b)
    12 {
    13     int x=find(a);
    14     int y=find(b);
    15     if(x==y) return;
    16     set[x]=y;
    17     
    18 }
    19 int main(void)
    20 {
    21     int t,n,m,a,b;
    22     char op;
    23     scanf("%d",&t);
    24     while(t--)
    25     {
    26         scanf("%d%d%*c",&n,&m);
    27         for(int i=0;i<=n+N;i++) set[i]=i;
    28         for(int i=0;i<m;i++){
    29             scanf("%c%d%d%*c",&op,&a,&b);
    30             if(op=='D'){
    31                 merge(a,N+b);
    32                 merge(b,N+a);
    33             }else{
    34                 int x=find(a);
    35                 int y=find(b);
    36                 if(x==y){
    37                     puts("In the same gang.");
    38                 }else if(x==find(b+N) || y==find(a+N)){
    39                     puts("In different gangs.");
    40                 }else puts("Not sure yet.");
    41             } 
    42         } 
    43     }
    44     return 0;
    45 }
    View Code

    另一种方法比较难,类似于带权并查集,为种类并查集,具体细节可以模拟数据体会:

     1 //Accepted    900K    297MS    C++    1051B    2014-03-28 16:12:36
     2 /*
     3     种类并查集。 
     4 */
     5 #include<stdio.h>
     6 #define N 100005
     7 int set[N],rank[N];
     8 int n,m;
     9 int find(int x)
    10 {
    11     if(x!=set[x]){
    12         int t=set[x];
    13         set[x]=find(set[x]);
    14         rank[x]=(rank[x]+rank[t])&1;
    15     }
    16     return set[x];
    17 }
    18 void merge(int a,int b)
    19 {
    20     int x=find(a);
    21     int y=find(b);
    22     if(x==y) return;
    23     set[y]=x;
    24     rank[y]=(rank[a]-rank[b]+1)&1;   
    25 }
    26 int main(void)
    27 {
    28     int t,a,b;
    29     char op;
    30     scanf("%d",&t);
    31     while(t--)
    32     {
    33         scanf("%d%d%*c",&n,&m);
    34         for(int i=0;i<=n;i++){
    35             rank[i]=0;set[i]=i;
    36         }
    37         for(int i=0;i<m;i++){
    38             scanf("%c%d%d%*c",&op,&a,&b);
    39             if(op=='A'){
    40                 int x=find(a);
    41                 int y=find(b);
    42                 if(x==y){
    43                     if(rank[a]==rank[b]) puts("In the same gang.");
    44                     else puts("In different gangs.");
    45                 }else puts("Not sure yet.");            
    46             }else{
    47                 merge(a,b);
    48             }
    49         }
    50     }
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    图的连通性问题之tarjan算法
    图的连通性问题之强连通分量初步
    NOIP 2010 引水入城
    最短路经典例题 codevs 1557 热浪
    图的连通性问题之连通和最小环
    最短路径算法
    《数据结构与算法-Javascript描述》
    蓝天白云
    《慢慢来,一切都还来得及》
    聚餐
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3630995.html
Copyright © 2011-2022 走看看