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. [vijos]lxhgww的奇思妙想(长链剖分)

    题意 题目链接 Sol 长链剖分 又是一个用各种花式技巧优化的暴力 它的主要思想是:对于每个节点,把深度最深的子节点当做重儿子,它们之间的边当做重边 这样就会有一些非常好的轻质 所有链长总和是\(O( ...

  2. StackTrack for debug

    System.Diagnostics.Debug.WriteLine("Serial port. {0},{1}", this.GetType().FullName, new Sy ...

  3. tomcat的相关使用

    tomcat服务器是apache下非常优秀的一款web服务器,当今的互联网企业中90%左右的中小型企业使用的都是tomcat.tomcat在部署项目时有很多很多的解决方案,这些你都清楚吗? 1.同一个 ...

  4. GitHub教程(三) 本地仓库托管到GitHub

    本文开头先特别声明一下:由于GitHub教程属于Git系列教程的GitHub子篇章,因此GitHub教程中将不再详细介绍Git操作命令及其用法,我会根据实际需要穿插着回顾Git操作命令.如果读者需要学 ...

  5. SPFieldLookupValue

    //得到查阅项的值SPWeb web = site.OpenWeb();SPList list = web.Lists["DemoList"];SPListItem item = ...

  6. JavaMail 的简单使用

    概述 邮件功能模块在大多数网站中,都是必不可少的功能模块.无论是用户注册还是重置密码,邮件都是比较常用的一个方式.本文主要介绍 JavaMail 的简单使用,方便大家快速开发,供大家参考.完整的 de ...

  7. http缓存基本介绍

    https://www.helloweba.com/view-blog-414.html

  8. 如何在 ubuntu linux 一行中执行多条指令

    cd /my_folder rm *.jar svn co path to repo mvn compile package install 使用&& 运算符连接指令 cd /my_f ...

  9. leetcode:栈

    1. evaluate-reverse-polish-notation Evaluate the value of an arithmetic expression in Reverse Polish ...

  10. java IO流——字节流

    字节流主要操作byte类型数据,以byte数组为准,主要操作类有InputStream(字节输入流).OutputSteam(字节输出流)由于IputStream和OutputStream都是抽象类, ...