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. 1
  2. 5 5
  3. A 1 2
  4. D 1 2
  5. A 1 2
  6. D 2 4
  7. A 1 4

Sample Output

  1. Not sure yet.
  2. In different gangs.
  3. In the same gang.

Source

 
这题自己当时想的时候总是想方设法去区分每个点是属于哪个集团,在第一次遇到D时就随机分两个集团,可是发现这样根本无法继续下去,因为如果出现的两个数字以前都没有出现过的话,怎么也无法确定各自集团的,后来网上看到了很多方法,但我觉得这个是最好的,最简单的思想,保留所有的可能性即可,没必要去纠结谁属于谁,把所有的可能都存储下来,每个点存储a,和a+n,a代表其属于某个集团,a+n属于另外一个集团,按照并查集存储并执行就好了。
  1. #include<iostream>
  2. #include<cstdio>
  3.  
  4. using namespace std;
  5.  
  6. int parent[];
  7.  
  8. int Getparent(int x)
  9. {
  10. if(x!=parent[x])
  11. parent[x] = Getparent(parent[x]);
  12. return parent[x];
  13. }
  14.  
  15. void Union(int x,int y)
  16. {
  17. int a = Getparent(x),b = Getparent(y);
  18. if(a!=b)
  19. parent[b] = a;
  20. }
  21.  
  22. int main()
  23. {
  24. int t;
  25. while(scanf("%d",&t)!=EOF)
  26. {
  27. while(t--)
  28. {
  29. int n,m;
  30. scanf("%d%d",&n,&m);
  31. for(int i=;i<n+n+;i++)
  32. {
  33. parent[i] = i;
  34. }
  35. for(int i=;i<m;i++)
  36. {
  37. char s;
  38. int a,b;
  39. getchar();
  40. scanf("%c %d %d",&s,&a,&b);
  41. if(s=='A')
  42. {
  43. if(Getparent(a) == Getparent(b)||Getparent(a+n)==Getparent(b+n))
  44. printf("In the same gang.\n");
  45. else if(Getparent(a) == Getparent(b+n)||Getparent(b) == Getparent(a+n))
  46. printf("In different gangs.\n");
  47. else
  48. printf("Not sure yet.\n");
  49. }
  50. else
  51. {
  52. Union(a,b+n);
  53. Union(a+n,b);
  54. }
  55. }
  56. }
  57. }
  58. return ;
  59. }

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. 《Java程序员面试笔试宝典》之为什么Java中有些接口没有任何方法

    由于Java不支持多重继承,即一个类只能有一个父类,为了克服单继承的缺点,Java语言引入了接口这一概念.接口是抽象方法定义的集合(接口中也可以定义一些常量值),是一种特殊的抽象类.接口中只包含方法的 ...

  2. Unity四种路径总结

    四种路径的权限:                                            Application.dataPath 包含游戏数据文件夹的路径(只读) Applicatio ...

  3. Java并发框架——AQS堵塞队列管理(一)——自旋锁

    我们知道一个线程在尝试获取锁失败后将被堵塞并增加等待队列中,它是一个如何的队列?又是如何管理此队列?这节聊聊CHL Node FIFO队列.  在谈到CHL Node FIFO队列之前,我们先分析这样 ...

  4. JSP九大内置对象和四种属性范围解读

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文首先主要解说了JSP中四种属性范围的概念.用法与实例. 然后在这个基础之上又引入了九 ...

  5. JavaScript获取某年某月的最后一天

    JavaScript获取某年某月的最后一天 1.实现源代码 <!DOCTYPE html> <!-- To change this license header, choose Li ...

  6. Juqery 中使用 ajax

    从 test.js 载入 JSON 数据,附加参数,显示 JSON 数据中一个 name 字段数据. jQuery 代码: $.getJSON("test.js", { name: ...

  7. sql server 删除所有表和存储过程

    1.删除外键约束 DECLARE c1 cursor for select 'alter table ['+ object_name(parent_obj) + '] drop constraint ...

  8. Android画一个随意拖动的圆形

    import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactor ...

  9. Linq 标准查询操作符三

    本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰 ...

  10. Sql server 数据库 单用户切换为多用户

    使用master 下的sysprocesses 查询 db正在使用的spid 如 select spid from sysprocesseswhere dbid=DB_ID('DbName') 然后执 ...