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;
}

  

最新文章

  1. Google V8编程详解(一)V8的编译安装(Ubuntu)
  2. 关于VS打开cshtml出现 未能完成该操作。无效指针
  3. Mac Pro 安装 cmake,报错 Warning: cmake-3.5.2 already installed, it&#39;s just not linked
  4. iOS 图片 的 聊天气泡显示 Objective-C
  5. Nhiberate (二) 搭项目
  6. 生成读取相关连接的物理地址的lib(动态导入库)和dll(动态链接库)
  7. ansible
  8. 百度贴吧图片抓取工具(Python)
  9. 源码解析之–网络层YTKNetwork
  10. nginx1.8+php5.6.10 服务器编译安装备忘2015-06
  11. 全文搜索-介绍-elasticsearch-definitive-guide翻译
  12. [笔记]NumPy基础操作
  13. fpm 制作 rpm 包
  14. SpringBoot中关于Mybatis使用的三个问题
  15. Ceva定理的四种证明方法
  16. zabbix 源码分析 another/first network error wait for 15s seconds 出现原因及调优建议
  17. js &amp; parseFloat &amp; toFixed
  18. 【BZOJ-2063】我爸是李刚 数位dp 好题
  19. 零基础学php的自学
  20. Linux 查看进程基本命令

热门文章

  1. Android 给EditText添加下划线
  2. mvc中尽量避免使用HttpContext.Current.Request
  3. python+selenium第一个脚本
  4. EasyUI手风琴 Tab卡使用
  5. COGS 678. 双重回文数
  6. TP5.1:依赖注入、绑定一个类到容器里、绑定一个闭包到容器中
  7. Win7无法连接wifi网络的解决方法
  8. SQL-常用数据类型
  9. Altium_Designer-怎么将“原理图的更改”更新到“pcb图”?
  10. 关于SAP UI5数据绑定我的一些原创内容