ACM题目————Find them, Catch them
Description
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
<= 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
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.
并查集的扩展。
//Asimple
#include <iostream>
#include <cstdio> using namespace std;
const int maxn = 100010;
int fa[maxn];//存贮根 fa[a] 存 a 的根
int r[maxn];// 存贮 fa[a] 与 a 的关系
// 0 则不在一个 gang 里, 1 表示在一个 gang 里
int T, n, m, a, b;
char ch; void make_set(int n)// 保存根
{
for(int i=1; i<=n; i++)
{
fa[i] = i ;// i 的根是 fa[i]
r[i] = 1 ;// 在同一个 gang 里
}
} int find_set(int a)// 找根节点
{
if( a == fa[a] ) return a;
else
{
int temp = fa[a] ;
fa[a] = find_set(fa[a]);
r[a] = (r[temp] + r[a] + 1 ) % 2 ;
}
return fa[a] ;
} void union_set(int a, int b)
{
int faa = find_set(a);//找根节点
int fbb = find_set(b);
if( faa != fbb )//两个根节点不同,就将其联合起来
{
fa[faa] = fbb ;
r[faa] = ( r[a] + r[b] ) % 2 ;//更新状态
}
} int main()
{
scanf("%d",&T);
while( T -- )
{
scanf("%d%d",&n,&m);
make_set(n);
while( m-- )
{
getchar();
scanf("%c%d%d",&ch,&a,&b);
if( ch == 'A' )
{
if( find_set(a) == find_set(b) )
{
if((r[a]+r[b])%2==0) cout << "In the same gang." << endl ;
else cout << "In different gangs." << endl ;
}
else cout << "Not sure yet." << endl ;
}
else union_set(a,b);
}
} return 0;
}
ACM题目————Find them, Catch them的更多相关文章
- ACM题目————中缀表达式转后缀
题目描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符在两 ...
- HDU ACM 题目分类
模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...
- ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]
原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...
- 有一种acm题目叫做,奇葩!
本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pi ...
- ACM题目————STL练习之求次数
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个 ...
- ACM题目————zoj问题
题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20322 解决:3560 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. ...
- ACM题目————又见拦截导弹
描述 大家对拦截导弹那个题目应该比较熟悉了,我再叙述一下题意:某国为了防御敌国的导弹袭击,新研制出来一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:它的第一发炮弹能够到达任意的高度,但是以后每一发炮 ...
- ACM题目————还是畅通工程
Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路 ...
- ACM题目————小A的计算器
Description 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-25分别由a-z表示. 现在小A要在这个操作系统上实现一个计算器,这 ...
随机推荐
- mysql及redis环境部署时遇到的问题解决
redis开启远程访问redis默认只允许本地访问,要使redis可以远程访问可以修改redis.conf打开redis.conf文件在NETWORK部分有说明 解决办法:注释掉bind 127.0. ...
- 测试App运行状态
示例代码: #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - ...
- Java基础之泛型——使用泛型链表类型(TryGenericLinkedList)
控制台程序 定义Point类: public class Point { // Create a point from its coordinates public Point(double xVal ...
- SQLSERVER:Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
背景: 在最近开发中遇到一个问题,对一个数据库进行操作时,我采用64个并行的任务每个任务保证一个数据库连接对象:但是每个任务内部均包含有24个文件需要读取,在读取文件之后,我们需要快速将这24个文件批 ...
- C++Primer 第十五章
//1.面向对象程序设计的核心思想是数据抽象,继承,动态绑定. // 通过使用数据抽象,我们可以将类的接口和实现分离 // 使用继承,可以定义相似的类型并对其相似关系建模 // 使用动态绑定,可以在一 ...
- 传递闭包+二进制位运算+floyd(poj2570)
Fiber Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3125 Accepted: 1436 Des ...
- CCF真题之日期计算
201509-2 日期计算 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 注意闰年的2月有29天.满足下面条件之一的是闰年: 1) 年份是4的整数倍,而且不是100的整数倍: 2 ...
- Android -- FragmentTabHost实现微信底部切换
1,在商城类的项目中我们开始一个项目的时候经常出现这样的需求,如下图所示: 下面使用户可以切换的模块,上面是对应的模块的详细内容,实现这个效果有很多方式,可以使用radiobutton+fragmen ...
- 关闭VS实时调试器
到注册表删除以下2个项目就可以了 HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/AeDebug/Debugger HK ...
- 对linux的根目录执行强制递归移除
开始开始时使用: #rm -f -r / 提示对根目录使用递归操作很危险,然后就没执行成功,让使用 --no-preserve-root 这个参数. 好吧,反正是虚拟机 于是执行: #rm -f -r ...