POJ 1703

题意:有两个犯罪团伙,所有犯罪成员编号从1-n,

D:代表两个成员一定不在同一个团伙

A:查询,做出相应的输出

思路: 若有两个犯罪成员a,b.  定义a与(b+n)是同一个犯罪团伙,b与(a+n)是同一个犯罪团伙。同一个犯罪团伙内

建立并查集,最后查询如果两个犯罪成员在同一个犯罪团伙则就是在一个犯罪团伙;如果(犯罪成员1)与((犯罪成员2)+n)在一个犯罪团伙,则说明犯罪成员1与犯罪成员2 不在同一个犯罪团伙;否则不确定两人是否在同一犯罪团伙;

注意:第一是并查集数组初始化时,是将1-2*n进行初始化,而不是习惯性的将1-n初始化;

第二    arr[x] = f(b + n);  arr[y] = f(a + n); (x == f( b+ n) || y == f(a + n),括号内的a,b一定是a,b而不能是x,y;

Find them, Catch them

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 42512 Accepted: 13086

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

POJ Monthly–2004.07.18

#include<stdio.h>
#include<string.h>
int arr[112345 * 2];
int f(int x)
{
    return arr[x] == x ? x : arr[x] = f(arr[x]);
}

int main()
{
    int t;
    scanf("%d", &t);

    while(t--)
    {
        int n, m;
        scanf("%d%d", &n, &m);

        for(int i = 0; i <= 2*n; i++)
        {
            arr[i] = i;
        }

        char c[2];
        int a, b;

        while(m--)
        {
            scanf("%s%d%d", c, &a, &b);

            int x = f(a);
            int y = f(b);

            if(c[0] == 'D')
            {

                if(x != y)
                {

                    arr[x] = f(b + n);
                    arr[y] = f(a + n);
                }
            }
            else
            {

                if(x == y)
                {
                    printf("In the same gang.\n");
                }
                else if(x == f( b+ n) || y == f(a + n))
                {
                    printf("In different gangs.\n");
                }
                else
                {
                    printf("Not sure yet.\n");
                }
            }
        }
    }

    return 0;
}

    原文作者:犯罪团伙问题
    原文地址: https://blog.csdn.net/Boy_i/article/details/53141385
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞