zoukankan      html  css  js  c++  java
  • POJ1703(2集合并查集)

    Find them, Catch them
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 39402   Accepted: 12101

    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.

    /*
        @ title : poj1703
        @ method : union-find sets
        @ result : Accepted    1948K    813MS    G++    967B
        @ auther : baneHunter
        @ date : 2016/3/19
    */
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int MAXN=100005;
    int par[MAXN+MAXN],rnk[MAXN+MAXN];
    int n,m;
    void prep()
    {
        for(int i=0;i<MAXN+MAXN;i++)
        {
            par[i]=i;
            rnk[i]=0;
        }
    }
    
    int fnd(int x)
    {
        if(x==par[x])
            return x;
        return par[x]=fnd(par[x]);
    }
    
    void unite(int x,int y)
    {
        int a=fnd(x);
        int b=fnd(y);
        if(a==b)    return ;
        if(rnk[a]<rnk[b])
        {
            par[a]=b;
        }    
        else
        {
            par[b]=a;
            if(rnk[a]==rnk[b])
                rnk[a]++;
        }
    }
    
    bool same(int x,int y)
    {
        return fnd(x)==fnd(y);
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            prep();
            while(m--)
            {
                scanf("%*c");
                char op;
                int x,y;
                scanf("%c",&op);
                scanf("%d%d",&x,&y);
                if(op=='A')    
                {
                    if(same(x,y))
                    {
                        printf("In the same gang.
    ");
                    }
                    else if(same(x,y+n))
                    {
                        printf("In different gangs.
    ");
                    }
                    else    printf("Not sure yet.
    ");
                }
                else
                {
                    unite(x+n,y);
                    unite(x,y+n);
                }
            }
        }
        
        return 0;
    }
  • 相关阅读:
    Entity Framework Core tools reference
    安装 SQL Server 并在 Ubuntu 上创建数据库
    Install Docker Engine on Ubuntu
    ubuntu 18.04 安装.net 5
    C# 远程服务器 安装、卸载 Windows 服务,读取远程注册表,关闭杀掉远程进程
    ASP.NET MVC ActionResult
    使用WCF实现消息推送
    postgresql 数据类型转换,日期操作函数
    sql 按姓氏笔画(拼音)排序
    postgresql 导入导出
  • 原文地址:https://www.cnblogs.com/program-ccc/p/4986062.html
Copyright © 2011-2022 走看看