• poj1703 Find them, Catch them 并查集


    poj(1703)

    Find them, Catch them
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 26992   Accepted: 8188

    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.

    题目大意:一个城市里有两个帮派,n代表帮派成员编号,有m次操作,D[a][b]代表a和b不是一个帮派,A[a][b]询问a和b是什么关系,即:关系不确定,在一个帮派,不再一个帮派。

    分析:并查集,用一个辅助数组pre[],(pre[a]=b||pre[b]=a)代表a的敌人是b,b的敌人是a,输入D的时候有四种情况构造并查集

    (1)若pre[a]==-1&&pre[b]==-1,则:pre[a]=b;pre[b]=a;

    (2)若pre[a]==-1&&pre[b]!=-1:则:pre[a]=b;make(a,pre[b]):把b的敌人和a并在一棵树里,即为朋友;

    (3)若pre[a]1=-1&&pre[b]==-1;则:同上;

    (4)若pre[a]!!=-1&&pre[b]!=-1:则:make(a,pre[b]),make(pre[a],b);

    对于每次A询问:

    若finde(a)==finde(b)一定是朋友;

    否则有两种情况,要么不确定,要么是敌人;

    当:finde(pre[a])==finde(b)即:a的老大的敌人与b的老大是朋友,则a和b一定是敌人

    否则是不确定的

    程序:

    #include"stdio.h"
    #include"string.h"
    #define M 100004
    int f[M];
    int pre[M];
    int finde(int x)
    {
        if(x!=f[x])
            f[x]=finde(f[x]);
        return f[x];
    }
    void make(int a,int b)
    {
        int x=finde(a);
        int y=finde(b);
        if(x!=y)
            f[x]=y;
    }
    int main()
    {
        int w,n,m,a,b,i;
        char ch[2];
        scanf("%d",&w);
        while(w--)
        {
            scanf("%d%d",&n,&m);
            for(i=0;i<=n;i++)
                f[i]=i;
            memset(pre,-1,sizeof(pre));
            while(m--)
            {
                scanf("%s%d%d",ch,&a,&b);
                if(ch[0]=='D')
                {
                    if(pre[a]==-1&&pre[b]==-1)
                    {
                        pre[a]=b;
                        pre[b]=a;
                    }
                    else if(pre[a]==-1&&pre[b]!=-1)
                    {
                        make(a,pre[b]);
                        pre[a]=b;
                    }
                    else if(pre[a]!=-1&&pre[b]==-1)
                    {
                        make(b,pre[a]);
                        pre[b]=a;
                    }
                    else
                    {
                        make(pre[a],b);
                        make(pre[b],a);
                    }
                }
                else
                {
                    if(finde(a)==finde(b))
                        printf("In the same gang.
    ");
                    else
                    {
                        if(finde(pre[a])==finde(b))
                            printf("In different gangs.
    ");
                        else
                            printf("Not sure yet.
    ");
                    }
                }
            }
        }
        return 0;
    }
    

  • 相关阅读:
    华为S570028TPLIAC 快速清除接口配置
    【银河麒麟操作系统V10】【服务器】 创建bond
    VMware Horizon 组件如何组成在一起
    Horizon桌面部署问题总结
    overlay网络技术之VxLAN详解
    Brocade 光纤交换机级联配置
    nginx反向代理400绕过学习
    dbeaver:可以作为简单报表、报警等用途的强大sql ide工具
    linuxpython2.x:安装cx_Oracle包:从最初、最原始的发行版状态:不依赖pip
    Linux 网卡限速:wondershaper 1.4.1@20211111:这是当前的最新版
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348267.html
Copyright © 2020-2023  润新知