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. 转:JAVA强制类型转换

    object对象转换为String的一些总结 ----------------------------------------------------------------------------- ...

  2. c# 隐藏 控制台应用程序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  3. Java基础(3):求任意数字的长度的三种方法

    题目:统计任意一个数字的长度 方法一:先把数字转换为字符换,再直接用现成的函数统计长度即可 方法二:用API public class Test { final static int[] sizeTa ...

  4. mvc3在window 7 iis7下以及 xp iis 5.1下的部署 ,asp.net MVC3无法打开项目文件E:/我们的项目/Project/HeatingMIS.Web/HeatingMIS.Web.csproj”。此安装不支持该项目类型。

    今天,小白来总结一下我在is上部署mvc3 .net 网站的时候的过程和遇到的问题. 其实,mvc的网站的部署跟平常的网站的部署都是一样的,只是下面有一些需要注意的地方. 1.应用程序池采用集成模式( ...

  5. java中时间的比较

    进入要比较从库中取出的时间,期初使用比较date.getTime()的值,但是当时间的年月日都相同时,时分秒较早的getTime()值比时分秒较晚的getTime()的值要大,至今笔者还不知这是为什么 ...

  6. phpredis 订阅者模式

    [TOC] 一.场景介绍 最近的一个项目需要用到发布/订阅的信息系统,以做到最新实时消息的通知.经查找后发现了redis pub/sub(发布/订阅的信息系统)可以满足我的开发需求,而且学习成本和使用 ...

  7. 怎样解决MySQL数据库主从复制延迟的问题---流行网站的解决办法(转载)

    像Facebook.开心001.人人网.优酷.豆瓣.淘宝等高流量.高并发的网站,单点数据库很难支撑得住,WEB2.0类型的网站中使用MySQL的 居多,要么用MySQL自带的MySQL NDB Clu ...

  8. paper 25 :SVM支持向量机是什么意思?

    转载来源:https://www.zhihu.com/question/21094489 作者:余洋链接:https://www.zhihu.com/question/21094489/answer/ ...

  9. HTML5的自定义属性data-* 的用法解析

    人们总喜欢往HTML标签上添加自定义属性来存储和操作数据.但这样做的问题是,你不知道将来会不会有其它脚本把你的自定义属性给重置掉,此外,你这样做也会导致html语法上不符合Html规范,以及一些其它副 ...

  10. libsvm

    代码文件主要针对Matlab进行说明,但个人仍觉得讲解的支持向量机内容非常棒,可以做为理解这一统计方法的辅助资料; LibSVM是台湾林智仁(Chih-Jen Lin)教授2001年开发的一套支持向量 ...