Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 41928   Accepted: 12886

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. 题目大意:判断俩人是否一个集团,但给的信息是谁和谁不是一个集团的,考的是祖宗节点的距离
题目链接:http://poj.org/problem?id=1703 代码:
#include <iostream>
#include <cstring>
#include <cstdio>
const int MAX=1e5+5;
using namespace std;
int rea[MAX],f[MAX];
int find(int n)
{
if(n==f[n])
return n;
int tmp=f[n];
f[n]=find(f[n]);
rea[n]=(rea[tmp]+rea[n])%2;
return f[n];
}
int main()
{ int t,n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
f[i]=i;
rea[i]=0;
}
char a[10];
int b,c;
for(int i=0;i<m;i++)
{
scanf("%s",a);
if(a[0]=='D')
{
scanf("%d%d",&b,&c);
int b1=find(b),c1=find(c);
if(b1!=c1)
{
f[b1]=c1;
rea[b1]=(rea[b]+rea[c]+1)%2;
}
}
else
{
scanf("%d%d",&b,&c);
int b1=find(b),c1=find(c);
if(b1==c1)
{
if(rea[b]==rea[c])
printf("In the same gang.\n");
else
printf("In different gangs.\n"); }
else
printf("Not sure yet.\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. poj.1703.Find them, Catch them(并查集)

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

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

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

  4. [并查集] POJ 1703 Find them, Catch them

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

  5. 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条臭 ...

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

    题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...

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

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

  8. POJ 1703 Find them, Catch them(确定元素归属集合的并查集)

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

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

  10. poj 1703 Find them, Catch them(种类并查集和一种巧妙的方法)

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

随机推荐

  1. NodeMCU初探

    对于ESP8266模块,早就想知道如何用其脚本语言, 自己先用的这个模块测试的 首先是先下载需要用到的工具和固件 链接:http://pan.baidu.com/s/1dF5NZ3N 密码:bziq ...

  2. [Java面试二]Java基础知识精华部分.

    一:java概述(快速浏览): 1991 年Sun公司的James Gosling等人开始开发名称为 Oak 的语言,希望用于控制嵌入在有线电视交换盒.PDA等的微处理器: 1994年将Oak语言更名 ...

  3. 【转】Spring MVC中Session的正确用法之我见

    Spring MVC是个非常优秀的框架,其优秀之处继承自Spring本身依赖注入(Dependency Injection)的强大的模块化和可配置性,其设计处处透露着易用性.可复用性与易集成性.优良的 ...

  4. LLBL Gen + Entity Framework 程序设计入门

    Entity Framework推出有好几年,除了微软的Visual Studio可以做实体框架开发外,第三方的开发工具如LLBL Gen, Devart Entity Developer也可以用来做 ...

  5. HTML学习目录

    前面的话 HTML被认为是前端知识体系里面最简单的知识,几年前,很多人都推荐在W3C上学习个几天就能够基本掌握.但随着HTML5和移动端的强势发展,HTML的技能点也越来越难.世上无难事,好学好总结. ...

  6. s2sh框架搭建(辅助工具:MyEclipse)及解决一些遇到的问题

    1.新建一个web project 2.首先生成Hibernate Facet 3.Hibernate Facet 安装步骤 4.然后是spring facet安装步骤 5.最后是struts fac ...

  7. JavaScript闭包(一)——实现

    闭包的官方的解释是:一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 通俗点的说法是: 从理论角度:所有的函数.因为它们都在创建的时候就将上层上下文 ...

  8. kqueue用法简介

    1.什么是kqueue和IO复用 kueue是在UNIX上比较高效的IO复用技术. 所谓的IO复用,就是同时等待多个文件描述符就绪,以系统调用的形式提供.如果所有文件描述符都没有就绪的话,该系统调用阻 ...

  9. Linux一块网卡添加多个IP地址

    环境: RHEL6.4 需求: Linux一块网卡添加多个IP地址 一.临时生效 1.1 网卡eth0添加一个IP地址 1.2 修改eth0:0的广播地址 二.永久生效 2.1 编辑ifcfg-eth ...

  10. [Azure附录]2.在Windows Server 2012中配置AD域服务

    <Windows Azure Platform 系列文章目录> 本章我们配置的AD域名为contoso.com 1.安装完AD域服务后,我们返回服务器管理器界面,点击"将此服务器 ...