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

  • 相关阅读:
    斐波那契数列实现方式,以及递归和非递归时间对比
    月份与季节
    时针与分针夹角
    二叉树非递归遍历 以及二叉树节点删除思路
    向左向右 —折半查找(二分法)
    c语言之字符串及字符集简介
    c语言之排序
    C语言代码页 预处理 和宏 结构体 共用体 枚举 指针简绍
    C语言之函数调用约定,递归,数组简介
    C语言之条件判断
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348267.html
Copyright © 2011-2022 走看看