Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 38668   Accepted: 11905

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. 看食物链那一题看了半天,对并查集的理解还是不够啊!这个取余的方法有点6
 #include <stdio.h>
#include <string.h>
const int maxn = ; int n,m;
int p[maxn];//保存父节点
int r[maxn];//保存与父节点的关系,0为同类。1为不同类 void make_set()
{
for(int i = ; i <= n; i++)
{
p[i] = i;
r[i] = ;
}
} int find(int x)
{
if(x == p[x])
return x;
int tmp = p[x]; //记录父亲节点,方便更新r[].
p[x] = find(p[x]);
r[x] = (r[x]+r[tmp])%; //根据子节点与父亲节点的关系和父节点与爷爷节点的关系,推导子节点与爷爷节点的关系
return p[x];
} void merge(int a, int b)
{
int fa = find(a);
int fb = find(b);
p[fa] = fb;
r[fa] = (r[a]+r[b]+)%; //fx与x关系 + x与y的关系 + y与fy的关系 = fx与fy的关系
} int main()
{
int test,a,b;
char str[];
freopen("in.txt","r",stdin);
scanf("%d",&test);
while(test--)
{
scanf("%d %d",&n,&m);
make_set();
while(m--)
{
scanf("%s %d %d",str,&a,&b); if(str[] == 'A')
{
if(find(a) != find(b)) //如果根节点不同,则不能判断关系
{
printf("Not sure yet.\n");
continue;
}
else
{
if(r[a] == r[b])
printf("In the same gang.\n");
else printf("In different gangs.\n");
}
}
else
{
merge(a,b);
}
}
}
return ;
}

Find them, Catch them(POJ 1703 关系并查集)的更多相关文章

  1. Poj(1703),种类并查集

    题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...

  2. poj 2492(关系并查集) 同性恋

    题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...

  3. poj 1182 (关系并查集) 食物链

    题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...

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

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

  5. 又见关系并查集 以POJ 1182 食物链为例

    简单的关系并查集一般非常easy依据给出的关系搞出一个有向的环,那么两者之间的关系就变成了两者之间的距离. 对于此题: 若u.v不在一个集合内,则显然此条语句会合法(暂且忽略后两条.下同). 那么将f ...

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

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

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

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

  8. poj 1182 食物链(关系并查集)

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

  9. pku 1703(种类并查集)

    题目链接:http://poj.org/problem?id=1703 思路;个人觉得本质上还是和带权并查集一样的,只不过多了一个MOD操作,然后就是向量关系图稍微改动一下就变成种类并查集了,对于本题 ...

随机推荐

  1. uva 227 Puzzle

     Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...

  2. Jasper_table_pass parameter to table component

    <subDataset name="Dataset1" uuid="2a894ef4-dbcc-47df-bfaf-027766c7352e"> 2 ...

  3. mysql 5.7 内存使用监控

    5.7 中的performance_schema 已经有能力监控mysql 的内存使用情况了,对于这一点也是要通过instrument 来实现的,由于内存这一块没有对应的consumer 所以只要 配 ...

  4. C51汇编伪指令

    1.DS ---预留存储区命令格式: [标号:] DS   表达式值其功能是从指定地址开始,定义一个存储区,以备源程序使用.存储区预留的存储单元数由表达式的值决定. ;从标号TEP地址处开始保留1个存 ...

  5. Qt5+VS2013兼容XP方法

    用Qt5+VS2013编译程序默认配置会在XP运行时报"不是有效的Win32程序"工作需要必须要XP运行 pro文件中加一句: 复制代码 QMAKE_LFLAGS_WINDOWS ...

  6. 可变参数列表-Java SE5新特性(转)

    Java1.5增加了新特性:可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理.注意:可变参数必须位于最后一项.当可变参数个数多于一个时,必将有一个不是最后一项,所以只支持 ...

  7. HDU4532(组合DP)

    题目:安排座位 解析:http://www.douban.com/note/269136472/ #include <iostream> #include <string.h> ...

  8. bst 二叉搜索树简单实现

    //数组实现二叉树: // 1.下标为零的元素为根节点,没有父节点 // 2.节点i的左儿子是2*i+1:右儿子2*i+2:父节点(i-1)/2: // 3.下标i为奇数则该节点有有兄弟,否则又左兄弟 ...

  9. Hive 3、Hive 的安装配置(本地derby模式)

    这种方式是最简单的存储方式,只需要在hive-site.xml做如下配置便可; $ vim hive-site.xml <configuration>   <property> ...

  10. 设置Eclipse启动JDK

    打开eclipse安装目录下的eclipse.ini文件,将红色内容加入 -vm ../Java/jdk1.6.0_26/bin (或者指向具体目录:D:/software/jdk_1.8u91/bi ...