Find them, Catch them

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 36488 Accepted: 11188

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

POJ Monthly–2004.07.18

题意:有两个犯罪团伙,D 操作就是表明两个人不是同一伙,A 查询两个人的关系

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define LL long long
using namespace std; const int MAX = 110000; struct node
{
int pre;
int rea;
}a[MAX]; int n,m; void init()
{
for(int i=0;i<=n;i++)
{
a[i].pre=i;
a[i].rea=0;
}
} int Find(int x)
{
if(a[x].pre==x)
{
return x;
}
int tmp=a[x].pre;
a[x].pre=Find(tmp);
a[x].rea=(a[tmp].rea+a[x].rea)%2;
return a[x].pre;
} void Join(int x,int y,int b,int c)
{
if(b!=c)
{
a[b].pre=c;
a[b].rea=(1+a[x].rea+2-a[y].rea)%2;
}
}
int main()
{
int T;
char s[10];
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
init();
int u,v;
for(int i=0;i<m;i++)
{
scanf("%s %d %d",s,&u,&v);
int b=Find(u);
int c=Find(v);
if(s[0]=='D')
{
Join(u,v,b,c);
}
else
{
if(b!=c)
{
printf("Not sure yet.\n");
}
else
{
if(a[u].rea==a[v].rea)
{
printf("In the same gang.\n");
}
else
{
printf("In different gangs.\n");
}
}
}
} }
return 0;
}

Find them, Catch them的更多相关文章

  1. SQLServer如何添加try catch

    在.net中我们经常用到try catch.不过在sqlserver中我们也可以使用try catch捕捉错误,在这里把语法记录下来和大家分享一下, --构建存储过程CREATE PROCEDURE ...

  2. try...catch..finally

    try..catch..finally try{ 代码块1 }catch(Exception e){ 代码块2 }finally{ 代码块3 } catch是抓取代码块1中的异常 代码块2是出异常后的 ...

  3. C++异常处理:try,catch,throw,finally的用法

    写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...

  4. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  5. [c#基础]关于try...catch最常见的笔试题

    引言 在翻看之前总结的常见面试题中,关于try...catch异常处理的还是蛮多了,今天看到这个面试题,也就重新学习一下. try..catch语法 try-catch语句由一个try块后跟一个或多个 ...

  6. 高程(4):执行环境、作用域、上下文执行过程、垃圾收集、try...catch...

    高程三 4.2.4.3 一.执行环境 1.全局执行环境是最外层的执行环境. 2.每个函数都有自己的执行环境,执行函数时,函数环境就会被推入一个当前环境栈中,执行完毕,栈将其环境弹出,把控制器返回给之前 ...

  7. try catch里面try catch嵌套

    try catch里能否内嵌try catch?答案是肯定的.但是等内层try catch出异常之后是个什么执行顺序呢?看下面代码 static void Main(string[] args) { ...

  8. 基础知识《十》java 异常捕捉 ( try catch finally ) 你真的掌握了吗?

    本文转载自  java 异常捕捉 ( try catch finally ) 你真的掌握了吗? 前言:java 中的异常处理机制你真的理解了吗?掌握了吗?catch 体里遇到 return 是怎么处理 ...

  9. java try(){}catch(){}自动资源释放

    从 Java 7 build 105 版本开始,Java 7 的编译器和运行环境支持新的 try-with-resources 语句,称为 ARM 块(Automatic Resource Manag ...

  10. Java throws Exception、try、catch

    throws Exception是方法后面接的 意思是向上级抛出异常 try{}里面的异常会被外面的catch捕捉到 抛出异常是throw new Exception("异常"); ...

随机推荐

  1. SQL 数据库 函数

    1.数学函数:操作一个数据,返回一个结果 --取上限ceiling select code,name,ceiling(price) from car ; --取下限 floor select floo ...

  2. 二分多重匹配(HDU5093)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  3. STL lower_bound upper_bound binary-search

    STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...

  4. 使用Sqlserver Management Studio 导入导出 Excel的方法

    之前 帮同事  导入sql server数据     本来打算用 C# 写程序导入的 后来发现网上的方法  貌似 都会对版本  限制来限制去的 看的我好头晕(吐槽一下  难道就没有一个 普遍的方法嘛, ...

  5. R12月末关帐的异常检查和处理

    在R12版本中月末关帐时经常会出现关不了的情况,而系统的异常报表的信息太过简单且不完全.结合项目本身发生的情况,做了以下的总结,希望能对公司其他R12项目有所启示. R12月度关帐的要点: 检查SLA ...

  6. struts拦截器

    struts中的拦截器相当于过滤器的作用 一在struts.xml中配置拦截器或拦截器栈 <interceptors>!--全部的拦截器 <interceptor name=&quo ...

  7. PTPX Power Analysis Flow

    PrimeTime PX工具是PrimeTime工具内的一个feature. PTPX的功耗分析,可以报告出chip,block,cell的各个level的功耗. 使用PTPX可以分析的功耗的方式: ...

  8. AHB中split机制简介

    完整的AHB协议:1)可以多个master,并且需要外加一个Arbiter,和write multiplexor.为了保证每一时刻只有一个master拥有访问权. 2)为了增强pipeline的能力, ...

  9. zw版【转发·台湾nvp系列Delphi例程】HALCON 3D Position Of Circles

    zw版[转发·台湾nvp系列Delphi例程]HALCON 3D Position Of Circles procedure TForm1.action();var ho_Image, ho_Elli ...

  10. js调用后台方法(如果你能容忍执行的后台方法变成一个常量)

    最近一直在做一个电话拨号的系统,系统不大,但是做的时间有点长了.其中用到了一个技术:js调用后台方法.解决这个问题花了不少时间,现如今仍然还有些不明白的地方,今天跟大家分享一下.真正明白的同学欢迎指正 ...