• Description POJ1703


    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

    题解

    并查集,如果我们有这样的关系D(a,b),D(b,c)

    考虑这样存储关系 mix(a,b+n),mix(a+n,b),mix(b,c+n),mix(b+n,c)  ,通过这样的存图,我们可以得到这样的集合(a,b+n,c)和(b,a+n,c+n)因此就可以找到正确的关系

    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int MAXN=1e5+10;
    int  pri[MAXN*2];
    int find(int x)
    {
        if(pri[x]==x )return x;
        return pri[x]=find(pri[x]);
    }
    void mix(int x,int y)
    {
        int a=find(x),b=find(y);
        if(a!=b)
        {
            pri[a]=b;
        }
    }
    int main()
    {
        int _;
        scanf("%d",&_);
        while(_--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            for (int i = 0; i <=n+n ; ++i) {
                pri[i]=i;
            }
            for (int i = 0; i <m ; ++i) {
                char S[2];
                scanf("%s",&S);
                if(S[0]=='A')
                {
                    int x,y;
                    scanf("%d%d",&x,&y);
                    if(n==2)
                    {
                        printf("In different gangs.
    ");
                    }
                    else if(find(x)==find(y))
                    {
    
                        printf("In the same gang.
    ");
                    }
                    else if(find(x)==find(y+n))//y+n如果和x在一个集合中那么y一定和x不在一个集合中,如果直接find(x)!=find(y) 可能会有些y的关系还没有清楚
                    {                          
                        printf("In different gangs.
    ");
                    }
                    else
                    {
                        printf("Not sure yet.
    ");
                    }
    
                }
                else if(S[0]=='D')
                {
                    int x, y;
                    scanf("%d%d",&x,&y);
                    mix(x,y+n);
                    mix(x+n,y);
                }
            }
        }
    
    
        return 0;
    }
    

      

  • 相关阅读:
    【译】常用网络端口号列表
    使用Simian进行重复代码检测
    使用GCOV进行代码覆盖率统计
    AFL Fuzz安装及完成一次简单的模糊测试
    数据可视化概述
    完成下方的 which_date() 函数,并返回某一起始时间后特定一段时间的日期
    linux用户不在sudoers文件中
    linux /lib64/libc.so.6: version `GLIBC_2.17′ not found
    web api 2.0 上传文件超过4M时,出现404错误
    Centos7 编译安装 Nginx Mariadb Asp.net Core2 (实测 笔记 Centos 7.7 + Openssl 1.1.1d + Mariadb 10.3.7 + Nginx 1.16.1 + Asp.net. Core 2 )
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9456819.html
Copyright © 2020-2023  润新知