Find them, Catch them
 
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 32225   Accepted: 9947

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.
  4.  
  5. 题目大意:N 表示共有 N 个帮派;下面有 M 条个询问
    有两个帮派,现在告诉你一些互相敌对的点对,然后让你判断某两个点之间的关系,并首先判两个点关系是否确定;
    用并查集的思想只要两个点在一个集合里就可以了,直接用并查集然后时候在同一个集合;
    AC代码:
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. using namespace std;
  5. // 0 代表木有关系,1表示不同类,2代表同类
  6. const int N = 1e5+;
  7. int m;
  8. int f[N],c[N];
  9. int init()
  10. {
  11. for(int i=; i<=m; i++)
  12. {
  13. f[i] = i;
  14. c[i] = ;
  15. }
  16. }
  17. int xfind(int x)
  18. {
  19. if(f[x] == x)
  20. return x;
  21. int t = f[x];
  22. f[x] =xfind(f[x]);
  23. c[x] = (c[x]+c[t])%;
  24. return f[x];
  25. }
  26. int solve(char ch,int x,int y )
  27. {
  28. int fx = xfind(x);
  29. int fy = xfind(y);
  30. if(fx != fy)
  31. {
  32. if(ch == 'A') return ; //返回值0,表示不确定
  33. f[fx] =fy;
  34. if((c[x]+c[y])% ==)
  35. c[fx] = ;
  36. }
  37. else
  38. {
  39. if(ch == 'A')
  40. {
  41. if(c[x] != c[y]) return ; //返回值1,表示在不同的帮派
  42. if(c[x] == c[y]) return ; //返回值2,表示在相同的帮派
  43. }
  44. }
  45. if(ch == 'D') return ;//这种情况不同在考虑的
  46. }
  47. int main( )
  48. {
  49. int T,n,x,y;
  50. char s;
  51. scanf("%d",&T);
  52. while(T--)
  53. {
  54. scanf("%d %d",&m,&n);
  55. init(); //初始化并查集
  56. for(int i=; i<n; i++)
  57. {
  58. scanf(" %c %d %d",&s,&x,&y);
  59. int flag = solve(s,x,y);
  60. if(flag == ) printf("Not sure yet.\n");
  61. else if(flag == ) printf("In different gangs.\n");
  62. else if(flag == ) printf("In the same gang.\n");
  63. }
  64. }
  65. return ;
  66. }

poj1703 Find them, Catch them(并查集的应用)的更多相关文章

  1. poj1703 Find them, Catch them 并查集

    poj(1703) Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26992   ...

  2. 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 th ...

  3. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  4. poj1703--Find them, Catch them(并查集应用)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32073   Accepted: ...

  5. POJ1703-Find them, Catch them 并查集构造

                                             Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...

  6. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  7. POJ 1703 Find them, Catch them 并查集的应用

    题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...

  8. poj1703_Find them, Catch them_并查集

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42451   Accepted: ...

  9. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  10. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

随机推荐

  1. Android内存优化11 内存泄漏常见情况2 内部类泄漏

    线程持久化 Java中的Thread有一个特点就是她们都是直接被GC Root所引用,也就是说Dalvik虚拟机对所有被激活状态的线程都是持有强引用,导致GC永远都无法回收掉这些线程对象,除非线程被手 ...

  2. osx的10款文本编辑器

    ) Eddie Eddie 是 Mac OS X 和 Gnome 下的一个程序员编辑器,灵感来自于 Macintosh Programmer’s Workshop, Eddie 包含很多强大的特性,而 ...

  3. javascript常用排序算法实现

    毕业后,由于工作中很少需要自已去写一些排序,所以那些排序算法都忘得差不多了,不过排序是最基础的算法,还是不能落下啦,于是找了一些资料,然后用Javascript实现了一些常用的算法,具体代码如下: & ...

  4. linux grep的选项

    grep  -i          关闭大写和小写敏感性 grep      -v    打印全部不包括. . 的行(屏蔽某些条目) grep     -l     打印包括模式的文件名称 grep  ...

  5. 【转】go语言的字节序

    原文:http://lihaoquan.me/2016/11/5/golang-byteorder.html 这个人的博客写的不错,品质也比较高. 我应该也要有这种精神,这种态度.深入到计算机的世界中 ...

  6. TCP的状态(SYN,FIN等)

    TCP的标志位有SYN,FIN,RST,ACK,PSH,URG SYN:建立连接. FIN:关闭连接. RST:连接重置. ACK:响应. PSH:有数据传输. URG:urgent紧急. ACK可以 ...

  7. 【甘道夫】Win7环境下Eclipse连接Hadoop2.2.0

    准备: 确保hadoop2.2.0集群正常执行 1.eclipse中建立javaproject,导入hadoop2.2.0相关jar包 2.在src根文件夹下拷入log4j.properties,通过 ...

  8. C#秘密武器之特性

    一.概述 Attribute说白了就是一个类而已,里边一般含有一些附加信息,或者一些特殊的处理逻辑,以便告诉编译器应用该特性的东东是个奇葩,需要特殊对待! 二.使用时的注意事项 2.1. Attrib ...

  9. vs2012升级到vs2013后,sql server 无法通过IP登录解决方案

    因项目需要,vs升级到2013,可是数据库通过IP却无法连接,一直报内存异常,困扰许久,原来是.net框架出了问题,可以通过用管理员身份运行:netsh winsock reset解决!

  10. 【转】Socket状态变迁图

    转自:http://www.cnblogs.com/ILove/archive/2008/12/08/1350430.html   服务端,端口的状态变化 先在本机(IP地址为:192.168.1.1 ...