Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 43132   Accepted: 13257

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

原题大意:对于T组数据,每组数据有两个整数n,m代表有n个点,m次询问。
              接下来m行,每一行第一个字符为A,则回答1与2是否为一个集合,回答是,不是,或是不知道。
              第一个字符为D,则表示1与2不是一个集合。
解题思路:此题唯一与裸并查集不同的就是D表示的是不同。
              明显的是1+0为1,(1+1)%2==0,明显的是组成了一个循环结构,可以用向量的方式来解。
#include<stdio.h>
int father[150500],relation[150050];
void init(int n)
{
int i;
for(i=1;i<=n;++i) father[i]=i;
for(i=1;i<=n;++i) relation[i]=0;
}
int find(int x)
{
int t;
if(x==father[x]) return (x);
t=father[x];
father[x]=find(t);
relation[x]=(relation[x]+relation[t])%2;
return (father[x]);
}
void merge(int x,int y)
{
int find_x=find(x);
int find_y=find(y);
if(find_x!=find_y)
{
relation[find_x]=(relation[y]+1-relation[x]+2)%2;
father[find_x]=find_y;
}
}
int main()
{
int T,n,m,num=0;char c;int x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init(n);
while(m--)
{
getchar();
scanf("%c%d%d",&c,&x,&y);
if(c=='D')
{
merge(x,y);
}else
if(c=='A')
{
if(find(x)!=find(y))
{
printf("Not sure yet.\n");
continue;
}else
if(relation[x]==relation[y])
{
printf("In the same gang.\n");
continue;
}else
if(relation[x]!=relation[y])
printf("In different gangs.\n");
}
}
}
return 0;
}

  

           

[并查集] POJ 1703 Find them, Catch them的更多相关文章

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

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

  2. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  3. POJ 1703 Find them, Catch them (数据结构-并查集)

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

  4. poj 1703 Find them, Catch them 【并查集 新写法的思路】

    题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...

  5. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

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

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

  7. POJ 1703 Find them, Catch them(种类并查集)

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

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

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

  9. (中等) POJ 1703 Find them, Catch them,带权并查集。

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

随机推荐

  1. winform 对话框,保存,另存为,还有打印控件

    学习的对话框的种类: 1.打开文件对话框(OpenFileDialog) 2.保存文件对话框(SaveFileDialog) 3.字体对话框(FontDialog) 4.颜色对话框(ColorDial ...

  2. 【状压DP】bzoj1087 互不侵犯king

    一.题目 Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上.下.左.右,以及左上.左下.右上.右下八个方向上附近的各一个格子,共8个格子. I ...

  3. python之实现基于paramiko和mysql数据库的堡垒机

    一.堡垒机结构 堡垒机执行流程: 管理员为用户在服务器上创建账号(将公钥放置服务器,或者使用用户名密码) 用户登陆堡垒机,输入堡垒机用户名密码,现实当前用户管理的服务器列表 用户选择服务器,并自动登陆 ...

  4. Git命令行下解决冲突

    使用Git时,在pull.merge.rebase的过程中,经常会遇到conflict的情况. 遇到conflict时,以上处理过程会终端,并且命令行中显示(xxx|MERGING)的状态(Windo ...

  5. 微信小程序-登陆、支付、模板消息

    wx.login(OBJECT) 调用接口获取登录凭证(code)进而换取用户登录态信息,包括用户的唯一标识(openid) 及本次登录的 会话密钥(session_key).用户数据的加解密通讯需要 ...

  6. fzuoj1111Radar Installation (贪心)

    题目大意是在海岸线布置n个雷达,要求雷达的范围要包含所有的小岛: 思路:逆向思维把小岛看成一个个范围,与海岸线的交集,从最左端的开始找 (贪心最左端的点),接着不用一个一个去遍历,直接用前一个的右端点 ...

  7. SVN版本号打包脚本工具

    做网页游戏开发的时候,经常会触及到对文件版本号的管理.最近由于做新项目的原因,把原来手写版本号的方法改进了一下,借由svn的版本号生成及用java写了个xml解析输出文件,把手动的东西都变成全自动. ...

  8. Jmeter学习(一)

    最近测了一个导出功能,感觉应该学习下Jmeter,WEB系统的性能系统还是需要有一定累积. 选择Jmeter而不是LR,很简单的原因是QTP和LR不能装一台机器上. 也有很多测试人员推荐Jmeter, ...

  9. (十五)ioctl、ifreq、ifconf

    ioctl操作   传统上ioctl函数是用于那些普遍使用,但不适合归入其他类别的任何特性的系统接 口.Posix去掉了ioctl,它通过 创建特殊的其功能已被Posix标准化的包裹函数来代替ioct ...

  10. ubuntu16.04中将python3设置为默认

    直接执行这两个命令即可: sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo upd ...