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要在这个操作系统上实现一个计算器,这 ...
随机推荐
- RPC、SQL、NFS属于OSI的哪一层
第一层:物理层 第二层:数据链路层 802.2.802.3ATM.HDLC.FRAME RELAY 第三层:网络层 IP.IPX.ARP.APPLETALK.ICMP 第四层:传输层 TCP.UDP. ...
- 由浅到深讲解C#-LINQ
在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...
- Java基础之创建窗口——颜色和光标(TryWindow4)
控制台程序. java.awt包中把SystemColor类定义为Color类的子类.SystemColor类封装了本机操作系统用于显示各种组件的标准颜色.如果要比较SystemColor值和Colo ...
- spring 小结
第一步:配置 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...
- [c++基本语法]——构造函数初始化列表
c++构造函数初始化成员变量列表: #pragma once class Node { public: int data; // 权值 Node *parent; // 父节点 Node *left; ...
- node.js学习(1)
新建便笺 3 node.js学习(1) 1)安装 http://nodejs.org/download/下载. 2)编写一个案例 var http=require("http"); ...
- 一步一步学习Swift之(一):关于swift与开发环境配置
一.什么是Swift? 1.Swift 是一种新的编程语言,用于编写 iOS 和 OS X 应用. 2.Swift 结合了 C 和 Objective-C 的优点并且不受 C 兼容性的限制. 3.Sw ...
- ads
(3) Make可以编译整个工程,并生成映像文件.在ADS中,ARM提供了三种映像文件, 1. Debug: 使用本生成目标生成的映像文件中包含了所有的调试信息,用于开发过程中使用 ...
- Android Support Font 安卓系统支持字体(配图)
测试了一台安卓机器,发现所有字体显示都一样.
- JQuery下CheckBox全选全不选反选
<script src="JS/jquery-1.7.1.js"></script> <script type="text/javascri ...