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要在这个操作系统上实现一个计算器,这 ...
随机推荐
- 计算NSString含有多少个相同字符串
// // NSString+NSStringExt.m // 01-NSString类 // // Created by apple on 14-3-20. // Copyright (c) ...
- <s:select>中list获取action中数据
jsp: <s:select name="cate1.id" list="cate1list" listKey="id" listVa ...
- 通常Struts框架会自动地从action mapping中创建action对象
开发者不必在Spring中去注册action,尽管可以这么去做,通常Struts框架会自动地从action mapping中创建action对象 struts2-spring-plugin-x-x-x ...
- Eclipse+SVN搭建开发环境
目前我们开发环境为:windows service 2008 r2 x64 现在开始记录一下eclipse+SVN环境搭建过程: 1,)下载 VisualSVN-Server-3.4.2-x64:ht ...
- Lintcode: Matrix Zigzag Traversal
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ZigZag-or ...
- TTabControl、TMemo组件(制作一个简单的多文本编辑框)
TTabControl包含一列字符串标签的tabs 每个标签控制一个对象 首先创建一个TForm;接下来添加TTabControl组件和一个文件对话框TOpenDialog(用于添加文件),然后在TT ...
- 2-sat(石头、剪刀、布)hdu4115
Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- [原创]java WEB学习笔记76:Hibernate学习之路---Hibernate介绍,hibernate 环境的搭建
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- .NET: C#: StopWatch
StopWatch class is used for calculate the timespan for that procedure. In Debug Mode it will be very ...
- ofbiz进击 。 ofbiz 退货流程(包含获取可退货项流程分析 以及 取消退货项的过程分析)
根据订单获取可退货项流程分析 退货的时候,调用 services_return.xml 中的获取可进行退货的退货项 getReturnableItems ,该服务调用了Java类 org.ofbi ...