zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    mysql通过一张表更新另一张表
    申请微信支付填错对公账号的解决办法
    radio 实现点击两次 第一次点击选中第二次点击取消
    C#修改下拉框选项的高度
    Centos7 用yum命令安装LAMP环境(php+Apache+Mysql)以及php扩展
    php备份数据库
    windows环境下 composer 的安装与使用
    PHP获取多维数据的交集与差集
    JS 图片懒加载
    搭建 window + nginx + php 开发环境
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9456819.html
Copyright © 2011-2022 走看看