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,b),D(b,c)

考虑这样存储关系 mix(a,b+n),mix(a+n,b),mix(b,c+n),mix(b+n,c)  ,通过这样的存图,我们可以得到这样的集合(a,b+n,c)和(b,a+n,c+n)因此就可以找到正确的关系

#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=1e5+10;
int pri[MAXN*2];
int find(int x)
{
if(pri[x]==x )return x;
return pri[x]=find(pri[x]);
}
void mix(int x,int y)
{
int a=find(x),b=find(y);
if(a!=b)
{
pri[a]=b;
}
}
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
int n,m;
scanf("%d%d",&n,&m);
for (int i = 0; i <=n+n ; ++i) {
pri[i]=i;
}
for (int i = 0; i <m ; ++i) {
char S[2];
scanf("%s",&S);
if(S[0]=='A')
{
int x,y;
scanf("%d%d",&x,&y);
if(n==2)
{
printf("In different gangs.\n");
}
else if(find(x)==find(y))
{ printf("In the same gang.\n");
}
else if(find(x)==find(y+n))//y+n如果和x在一个集合中那么y一定和x不在一个集合中,如果直接find(x)!=find(y) 可能会有些y的关系还没有清楚
{
printf("In different gangs.\n");
}
else
{
printf("Not sure yet.\n");
} }
else if(S[0]=='D')
{
int x, y;
scanf("%d%d",&x,&y);
mix(x,y+n);
mix(x+n,y);
}
}
} return 0;
}

  

Description POJ1703的更多相关文章

  1. POJ1703(2集合并查集)

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

  2. 完美解决CodeSmith无法获取MySQL表及列Description说明注释的方案

    问题描述: CodeSmith是现在比较实用的代码生成器,但是我们发现一个问题: 使用CodeSmith编写MySQL模板的时候,会发现一个问题:MySQL数据表中的列说明获取不到,也就是column ...

  3. 资源描述结构(Resource Description Framework,RDF)

    资源描述框架(Resource Description Framework),一种用于描述Web资源的标记语言.RDF是一个处理元数据的XML(标准通用标记语言的子集)应用,所谓元数据,就是" ...

  4. MVC中得到成员元数据的Description特性描述信息公用方法

    #region 从类型成员获取指定的Attribute T特性集合 /// <summary> /// 从类型成员获取指定的Attribute T特性集合 /// </summary ...

  5. 枚举扩展方法获取枚举Description

    枚举扩展方法 /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="v ...

  6. 什么情况下才要重写Objective-C中的description方法

    特别注意: 千万不要在description方法中同时使用%@和self,同时使用了%@和self,代表要调用self的description方法,因此最终会导致程序陷入死循环,循环调用descrip ...

  7. 启用WebApi 2里的Api描述信息(Help下的Description)

    环境:vs2013+web api 2 问题:默认情况下新建的Web Api 2项目,自带的Help页下会显示Api的相关信息,但Description那一栏无法获取到数据,如下图所示: 解决: 1. ...

  8. Description Resource Path Location Type Error executing aapt: Return code -1073741819 Client line 1

    Logcat报错:Description    Resource    Path    Location Type Error executing aapt: Return code -1073741 ...

  9. C# Enum Name String Description之间的相互转换

    最近工作中经常用到Enum中Value.String.Description之间的相互转换,特此总结一下. 1.首先定义Enum对象 public enum Weekday { [Descriptio ...

随机推荐

  1. 1.字符串池化(intern)机制及拓展学习

    1.字符串intern机制 用了这么久的python,时刻和字符串打交道,直到遇到下面的情况: a = "hello" b = "hello" print(a ...

  2. HTML头部元素实例

    HTML head 元素 标签 描述 <head> 定义了文档的信息 <title> 定义了文档的标题 <base> 定义了页面链接标签的默认链接地址 <li ...

  3. Sleep 和 Wait 关于锁释放的区别

    sleep和wait的区别是一个老生常谈的问题.Sleep 是 Thread类的方法, wait是Object类的方法.但是关键的区别是对锁的操作问题. 当我们调用sleep的时候,线程进入休眠,但是 ...

  4. 《最牛B的Linux Shell命令》笔记

    1.以sudo 运行上一条命令 sudo !! 大家应该都知sudo,不解释.但通常出现的情况是,敲完命令执行后报错才发现忘了sudo.如下: ➜ ~ cp ~/download/CentOS7-Ba ...

  5. javascript代码工具库

    1. 垃圾收集 另一个块作用域非常有用的原因和闭包及回收内存垃圾的回收机制相关.这里简要说明一 下,而内部的实现原理,也就是闭包的机制会在第 5 章详细解释. 考虑以下代码: function pro ...

  6. Python模块与函数

    python的程序由包(package).模块(module)和函数组成.模块是处理某一类问题的集合,模块由函数和类组成,包是由一系列模块组成的集合.包必须至少包含一个__init__.py文件,该文 ...

  7. IOS 拦截所有push进来的子控制器

    /** * 能拦截所有push进来的子控制器 */ - (void)pushViewController:(UIViewController *)viewController animated:(BO ...

  8. 谷歌Web中文开发手冊:3响应式

    https://developers.google.com/web/fundamentals/getting-started/your-first-multi-screen-site/responsi ...

  9. LA 3938 动态最大连续和

    题目链接:https://vjudge.net/contest/146667#problem/C 题意:动态的求一个区间的最大连续和. 分析: 看上去可以RMQ去做,但是,当分成两个部分,原来的部分的 ...

  10. 【转】Android开发学习总结(一)——搭建最新版本的Android开发环境

    最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是一直没有搭建开发环境去学习,Android的更新速度比较快了,Android1. ...