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. 04747_Java语言程序设计(一)_第8章_多线程

    例8.1应用程序用Thread子类实现多线程. import java.util.Date; public class Example8_1 { static Athread threadA; sta ...

  2. 【转】linux 关机命令总结

    linux下常用的关机命令有:shutdown.halt.poweroff.init:重启命令有:reboot.下面本文就主要介绍一些常用的关机命令以及各种关机命令之间的区别和具体用法. 首先来看一下 ...

  3. JQuery 补充

    筛选: expr         String 字符串值,包含供匹配当前元素集合的选择器表达式. jQuery      objectobject 现有的jQuery对象,以匹配当前的元素. elem ...

  4. [Javascript] property function && Enumeration

    var vehicle3 = { type: "Submarine", capacity: 8, storedAt: "Underwater Outpost", ...

  5. iOS textfield限制长度,中文占2字符,英文占1字符

    之前遇到一种情况,限制textfield长度,并且要适配多语言,做到,例如中文占2字符,英文占1字符,还有考虑其他语言,网上找了很多方法,不太合适,最后结合网上的方案,修改出了还比较适用. 首先,增加 ...

  6. Func 委托 和 Action 委托 初步谈论

    继上篇EventHandler之后,继续填坑,简单了解下Func<TResult> 委托 和 Action 委托. msdn对于两者的解释: Func<TResult>:封装一 ...

  7. java序列化ClassNotFoundException

    简单的想从保存的对象中重新解析出对象,用了逆序列化,可是报错: java.lang.ClassNotFoundException: xxxxxxxxxxxx at java.net.URLClassL ...

  8. T4模板试水篇1_入门

    T4模板作为VS自带的一套代码生成器,功能有多强大我也不知道,最近查找了一些资料学习一下,做个笔记 更详细的资料参见: MSDN: http://msdn.microsoft.com/zh-cn/li ...

  9. commit后数据库干的工作

    用户提交commit后,数据库干的工作有: 1,oracle为用户的transaction生成一个SCN号. 2,LGWR把redo buffer中的数据写入到redo log file,同时把SCN ...

  10. MJRefresh(上拉加载下拉刷新)

    整理自:https://github.com/CoderMJLee/MJRefresh#%E6%94%AF%E6%8C%81%E5%93%AA%E4%BA%9B%E6%8E%A7%E4%BB%B6%E ...