poj(1703)

Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26992   Accepted: 8188

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.

题目大意:一个城市里有两个帮派,n代表帮派成员编号,有m次操作,D[a][b]代表a和b不是一个帮派,A[a][b]询问a和b是什么关系,即:关系不确定,在一个帮派,不再一个帮派。

分析:并查集,用一个辅助数组pre[],(pre[a]=b||pre[b]=a)代表a的敌人是b,b的敌人是a,输入D的时候有四种情况构造并查集

(1)若pre[a]==-1&&pre[b]==-1,则:pre[a]=b;pre[b]=a;

(2)若pre[a]==-1&&pre[b]!=-1:则:pre[a]=b;make(a,pre[b]):把b的敌人和a并在一棵树里,即为朋友;

(3)若pre[a]1=-1&&pre[b]==-1;则:同上;

(4)若pre[a]!!=-1&&pre[b]!=-1:则:make(a,pre[b]),make(pre[a],b);

对于每次A询问:

若finde(a)==finde(b)一定是朋友;

否则有两种情况,要么不确定,要么是敌人;

当:finde(pre[a])==finde(b)即:a的老大的敌人与b的老大是朋友,则a和b一定是敌人

否则是不确定的

程序:

#include"stdio.h"
#include"string.h"
#define M 100004
int f[M];
int pre[M];
int finde(int x)
{
if(x!=f[x])
f[x]=finde(f[x]);
return f[x];
}
void make(int a,int b)
{
int x=finde(a);
int y=finde(b);
if(x!=y)
f[x]=y;
}
int main()
{
int w,n,m,a,b,i;
char ch[2];
scanf("%d",&w);
while(w--)
{
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
f[i]=i;
memset(pre,-1,sizeof(pre));
while(m--)
{
scanf("%s%d%d",ch,&a,&b);
if(ch[0]=='D')
{
if(pre[a]==-1&&pre[b]==-1)
{
pre[a]=b;
pre[b]=a;
}
else if(pre[a]==-1&&pre[b]!=-1)
{
make(a,pre[b]);
pre[a]=b;
}
else if(pre[a]!=-1&&pre[b]==-1)
{
make(b,pre[a]);
pre[b]=a;
}
else
{
make(pre[a],b);
make(pre[b],a);
}
}
else
{
if(finde(a)==finde(b))
printf("In the same gang.\n");
else
{
if(finde(pre[a])==finde(b))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
}
}
}
return 0;
}

poj1703 Find them, Catch them 并查集的更多相关文章

  1. 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 ...

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

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

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

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

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

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

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

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

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

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

  7. poj1703_Find them, Catch them_并查集

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

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

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

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

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

随机推荐

  1. linux指定目录安装软件后,程序找不到共享库问题

    以svn为例,64位centos yum install subversion --installroot=/usr/svn/后 执行svn命令,报错svn: error while loading ...

  2. SQuirreL 连接 hive

    软件安装版本: hadoop-2.5.1 hbase-0.98.12.1-hadoop2 apache-hive-1.2.1-bin SQuirreL SQL Client3.7 集成步骤: 1. S ...

  3. go liteIDE

    go  liteIDE 1 COMM FILE package pricetable import ( "fmt" "math" "os" ...

  4. USBDongle及Btool使用说明

    BLE 模块可使用开发套件中的 USB Dongle 模拟手机APP配合Btool.exe 进行蓝牙通讯测试. >连接 BLE 模块 USB Dongle 和模块的连接是通讯的基础,扫描连接的操 ...

  5. Qt持久性对象进行序列化

    Mfc和Java中自定义类的对象都可以对其进行持久性保存,Qt持久性对象进行序列化当然也是必不可少的.不过这个问题还真困扰了我很长时间……Mfc通过重写虚函数Serialize().Java则是所属的 ...

  6. Delphi FindowWindow,FindowWindowEx

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  7. SQL SERVER中非聚集索引的覆盖,连接,交叉,过滤

    1.覆盖索引:select和where中包含的结果集中应存在“非聚集索引列”,这样就不用查找基表了,索引表即可搞定:   2.索引交叉:索引的交叉可以理解成建立多个非聚集索引之间的join,如表实体一 ...

  8. Spring冲刺阶段二(1)

    我们首先是根据同学们的评价来找补自己的不足之处,其中同学们反应最多的是我们的界面不够好看,但是我觉得虽然不好看但算是比较简洁.其次是没有体现内网在其中发挥的作用,这点我们还没有实现. 根据这些不足之处 ...

  9. 常用分类列表wp_list_categories()

    使用: <ul> <?php $args= array( 'depth'=>1, 'orderby'=>id, 'style'=>none ); wp_list_c ...

  10. new和delete malloc和free

    程序中动态分配的对象存放在自由存储区(free store)或堆(heap). C语言程序使用一对标准库函数malloc和free在自由存储区中分配存储空间,而C++语言则使用new和delete表达 ...