码迷,mamicode.com
首页 > 其他好文 > 详细

Find them, Catch them

时间:2014-06-02 09:38:19      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

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.

bubuko.com,布布扣
 1 #include"iostream"
 2 using namespace std;
 3 int p[100004],rela[100004];
 4 int t,n,m,a,b;
 5 char flag;
 6 void make()
 7 {
 8     for(int i=1;i<=n;i++)
 9     {
10         p[i]=i;
11         rela[i]=0;
12     }
13 } 
14 int find(int x)
15 {
16     int temp=p[x];
17     if(x==p[x])
18        return x;
19     p[x]=find(p[x]);
20     rela[x]=(rela[x]==rela[temp])?0:1;
21     return p[x];   
22 }
23 void unionset(int x,int y,int px,int py)
24 {
25     p[px]=py;
26     rela[px]=(rela[x]==rela[y])?1:0;
27 }
28 int main()
29 {
30     cin>>t;
31     while(t--)
32     {
33         cin>>n>>m;
34         make();
35         while(m--)
36         {
37             getchar();
38             scanf("%c%d%d",&flag,&a,&b);
39             int pa=find(a);
40             int pb=find(b);
41             if(flag==A)
42             {
43                 if(pa!=pb)
44                 {
45                     cout<<"Not sure yet."<<endl;
46                     continue;
47                 }
48                 if(rela[a]==rela[b])
49                 {
50                     cout<<"In the same gang."<<endl;
51                     continue;
52                 }
53                 cout<<"In different gangs."<<endl;
54                 continue;
55             }
56             if(flag==D)
57             {
58                 if(p[a]!=p[b])
59                   unionset(a,b,pa,pb);
60             }
61         }
62     }
63     return 0;
64 }
View Code

 

Find them, Catch them,布布扣,bubuko.com

Find them, Catch them

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/767355675hutaishi/p/3763477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!