Find them, Catch them

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.

 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 }

时间: 2024-08-26 22:57:48

Find them, Catch them的相关文章

Catch Application Exceptions in a Windows Forms Application

You need to handle the System.Windows.Forms.Application.ThreadException event for Windows Forms. This article really helped me: http://bytes.com/forum/thread236199.html. Application.ThreadException += new ThreadExceptionEventHandler(MyCommonException

try~Catch语句中异常的处理过程

[2014/10/12 21:40]文章待续~ 1.函数自身捕获处理异常的情况 下面的例子介绍了try~catch语句中出现异常时语句的执行顺序: package month10; import java.lang.*; public class TryCatch{ /* * 函数产生一个ArithmeticException异常 */ public static void First(){ System.out.println("第一个异常处理的例子"); try{ //double

android在程序崩溃时Catch异常并处理

Android系统的"程序异常退出",给应用的用户体验造成不良影响.为了捕获应用运行时异常并给出友好提示,便可继承UncaughtExceptionHandler类来处理.通过Thread.setDefaultUncaughtExceptionHandler()方法将异常处理类设置到线程上即可. 写一个例子来理解. 1.新建项目,新建一个MyCatchException类,实现uncaughtExceptionHandler. //全部错误捕捉器 public class MyCatc

有return的情况下try catch finally的执行顺序

http://www.cnblogs.com/lanxuezaipiao/p/3440471.html?cm_mc_uid=89442383850615035911861&cm_mc_sid_50200000=1505491196 1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么

[[email&#160;protected]] Omit catch error block if not needed

From [email protected], you can omit catch error block. Before: try { throw new Error('whatever'); } catch(err) { console.log(err) } Now: try { throw new Error('whatever'); } catch { console.log("error happened") } It is just a syntax sugar, if

Catch That Cow(广搜)

个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and t

poj 1703 Find them, Catch them

Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46119   Accepted: 14193 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 Drago

coverity&amp;fortify1--Poor Error Handling: Overly Broad Catch

1.描述: 多个 catch 块看上去既难看又繁琐,但使用一个"简约"的 catch 块捕获高级别的异常类(如 Exception),可能会混淆那些需要特殊处理的异常,或是捕获了不应在程序中这一点捕获的异常.本质上,捕获范围过大的异常与"Java 分类定义异常"这一目的是相违背的. 2.风险: 随着程序的增加而抛出新异常时,这种做法会十分危险.而新发生的异常类型也不会被注意到. 3.例子: try{ //IOoperation // } catch(Exceptio

bfs hdu 2717 Catch That Cow

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14553    Accepted Submission(s): 4422 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

hdu 2717 Catch That Cow

---恢复内容开始--- Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14433    Accepted Submission(s): 4396 Problem Description Farmer John has been informed of the location of a fugitive