【冰茶几专题】C - Find them, Catch them

C - Find them, Catch them
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit  Status

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.


种类冰茶几...看到还有一种算是拓展的交加权冰茶几?
看到有做法是在开一个数组。。。记录是哪一组....
但是因为只有两组....我们可以分别存...
因为不知道每一个D的两个人分别是哪个组(帮派?)
可以都存一下。
TLE了两次....应该是用了cin的事。。。改成scanf就变WA了。。。
想了下。原来是我对“not sure yet”的判断出现失误。
我开了一个v数组,记录在D下出现的人。
我误以为出现的人的帮派一定是确定的。
实际上并不是。
比如 1,3 5,7 3和7都出现了。但是3和7是一组与否显然还是“not sure yet”


#include <iostream>

#include <algorithm>

#include <cstring>

#include <cstdio>

#include <cmath>



using namespace std;



const int N=2E5+7;

bool v[N];

int f[N];

int T,n,m,x,y;

char ch;



int root (int x)

{

    if (f[x]!=x)

        f[x] = root(f[x]);

    return f[x];

}

void u(int a,int b)

{

    f[root(a)]=root(b);

}

void ask(int a,int b)

{

    //if (!v[a]||!v[b])

   // {

        

     //   return;

  //  }

    if (root(a)==root(b)||root(a+n)==root(b+n))

    {

        cout<<"In the same gang."<<endl;

    }

    else if (root(a)==root(b+n)||root(a+n)==root(b))

    {

        cout<<"In different gangs."<<endl;

    }

    else

    {

        cout<<"Not sure yet."<<endl;

    }

}





int main()

{

    cin>>T;

    while (T--)

    {

        memset(v,false,sizeof(v));

        for ( int i = 0 ; i < N ; i++ )

            f[i] = i;

        scanf("%d %d",&n,&m);

        for ( int i = 1 ; i <= m ; i++ )

        {



            scanf("%s %d %d",&ch,&x,&y);

            if (ch=='A')

            {

                ask(x,y);

            }

            else

            {

                v[x] = true;

                v[y] = true;

                u(x,y+n);

                u(x+n,y);

            }

        }

    }

    return 0;

}

 

 

 

你可能感兴趣的:(catch)