Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 31840   Accepted: 9807

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

 
这题自己当时想的时候总是想方设法去区分每个点是属于哪个集团,在第一次遇到D时就随机分两个集团,可是发现这样根本无法继续下去,因为如果出现的两个数字以前都没有出现过的话,怎么也无法确定各自集团的,后来网上看到了很多方法,但我觉得这个是最好的,最简单的思想,保留所有的可能性即可,没必要去纠结谁属于谁,把所有的可能都存储下来,每个点存储a,和a+n,a代表其属于某个集团,a+n属于另外一个集团,按照并查集存储并执行就好了。
 #include<iostream>
#include<cstdio> using namespace std; int parent[]; int Getparent(int x)
{
if(x!=parent[x])
parent[x] = Getparent(parent[x]);
return parent[x];
} void Union(int x,int y)
{
int a = Getparent(x),b = Getparent(y);
if(a!=b)
parent[b] = a;
} int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<n+n+;i++)
{
parent[i] = i;
}
for(int i=;i<m;i++)
{
char s;
int a,b;
getchar();
scanf("%c %d %d",&s,&a,&b);
if(s=='A')
{
if(Getparent(a) == Getparent(b)||Getparent(a+n)==Getparent(b+n))
printf("In the same gang.\n");
else if(Getparent(a) == Getparent(b+n)||Getparent(b) == Getparent(a+n))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
else
{
Union(a,b+n);
Union(a+n,b);
}
}
}
}
return ;
}

poj 1703(带权并查集)的更多相关文章

  1. POJ 1703 带权并查集

    直接解释输入了: 第一行cases. 然后是n和m代表有n个人,m个操作 给你两个空的集合 每个操作后面跟着俩数 D操作是说这俩数不在一个集合里. A操作问这俩数什么关系 不能确定:输出Not sur ...

  2. poj 1182 (带权并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 71361   Accepted: 21131 Description ...

  3. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  4. Navigation Nightmare POJ - 1984 带权并查集

    #include<iostream> #include<cmath> #include<algorithm> using namespace std; ; // 东 ...

  5. Parity game POJ - 1733 带权并查集

    #include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...

  6. K - Find them, Catch them POJ - 1703 (带权并查集)

    题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...

  7. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  8. (中等) POJ 1703 Find them, Catch them,带权并查集。

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

  9. poj 1703 - Find them, Catch them【带权并查集】

    <题目链接> 题目大意: 已知所有元素要么属于第一个集合,要么属于第二个集合,给出两种操作.第一种是D a b,表示a,b两个元素不在一个集合里面.第二种操作是A a b,表示询问a,b两 ...

  10. POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】

      The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...

随机推荐

  1. [置顶] vi、akw和sed总结

  2. [Oracle] 参数修改小结

    v$parameter Oracle参数的修改比较复杂,有些参数是可以在session级别修改,有些则必须在system级别修改,有些参数修改后马上生效(不需要重启),有些参数则必须重启才能生效,那么 ...

  3. WEB程序会话管理--HttpSession和Cookie

    WEB应用的会话管理的原理: 由于WEB应用的请求和响应是基于HTTP的,而HTTP由属于无状态的通信协议,只能记录本次请求的信息,因此服务器不会记住这一次的请求和下一次请求的关系.所以会话管理的原理 ...

  4. JQuery 选择器 *很重要 多记

    1)基本选择器: 跟CSS选择器类似 2) 层次选择器 div>span   紧接这div同一级下的全部span .one+div     同一等级的div #two~div    同一等级di ...

  5. pyqt一个简单的动画

    import sys from PyQt4.QtGui import QApplication , QGraphicsEllipseItem , QGraphicsItemAnimationfrom ...

  6. iOS开发之Crash分析,以及收集

    一  先谈谈iOS的Crash收集方式: 1. APP 发生crash,用户手机手机上肯定会有crash纪录,当然删除了该app,或是删了再装 crash纪录还是没了. 2. 如果用户设置-隐私  同 ...

  7. EffectiveC#03--用委托表示回调,用事件定义对外接口

    1.回调的场景:我给了儿子一个任务且他可以报告状态来(重复的)打断我.而我在等待他完成任务的每一个部份时不用阻塞我自己的进程.他可以在有重要(或者事件)状态报告时,可以定时的打断我,或者向我询求帮助 ...

  8. Vim知识点收集

    (注意: 只记录工作中实际使用的命令) 删除带有pattern的所有行    :g/pattern/d 删除不带pattern的所有行   :g!/pattern/d 匹配red和blue,无次序   ...

  9. 使用正则表达式限制swing (JTextField等) 的输入

    之前使用Qt编写Gui程序的时候,可以直接使用正则表达式限制所有输入框,非常方便. 这段时间要做一份课程设计,使用java编写,ui要限制输入,比如只能输入x位数字,输入身份证等. 百度了许多资料,发 ...

  10. dropdownlist绑定和选中

    最近在使用dropdownlist控件,对于这个控件,目前我知道的会使用两种方式去绑定数据,现在将这两种方式分享给大家: 现在是后台数据绑定 protected void BindCarID() { ...