ACM Find them, Catch them
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
Output
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.
/*
Name: Find them, Catch them
Copyright:
Author:
Date: 09/08/17 09:27
Description: 给定两个集合,通过已给信息,判断两个元素是否同一集合。
如果是,则输出 "In the same gang."
如果不是,则输出"In different gangs."
如果不能从已给信息判断,则输出 "Not sure yet."
*/
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int N = ;
int parent[N],rank[N]; void init() /*初始化*/
{
for(int i = ; i < N; i++)
{
parent[i] = i;
rank[i] = -;
} }
int find(int n)
{
if(n !=parent[n])
parent[n] = find(parent[n]);
return parent[n];
}
/*
使用这个函数竟然给我TLE = =、
int find(int n)
{
if(n == parent[n])
return n;
else
return find(parent[n]);
}
*/
void merge(int a,int b)
{
a = find(a);
b = find(b);
if(a != b)
parent[a] = b;
}
int main()
{
int t,m,n,a,b;
char ch;
while(cin>>t)
{
while(t--)
{
int temp = ;
init();
scanf("%d %d",&n,&m);
for(int i = ; i < m; i++)
{
getchar();
scanf("%c %d %d",&ch,&a,&b);
//cin>>Node[i].ch>>Node[i].a>>Node[i].b; 在循环次数较多情况下使用scanf 使用cin容易TLE!
if(ch == 'D')
{
if(rank[a] != -) merge(rank[a],b);
if(rank[b] != -) merge(rank[b],a);
rank[a] = b;
rank[b] = a; }else{
if( find(a) == find(b))
printf("In the same gang.\n");
else if(find(rank[a]) == find(b))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
}
}
}
return ;
}
ACM Find them, Catch them的更多相关文章
- ACM题目————Find them, Catch them
Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 手把手教你用C++ 写ACM自动刷题神器(冲入HDU首页)
转载注明原地址:http://blog.csdn.net/nk_test/article/details/49497017 少年,作为苦练ACM,通宵刷题的你 是不是想着有一天能够荣登各大OJ榜首,俯 ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- HDU 2717 Catch That Cow(常规bfs)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...
- ACM卡常数(各种玄学优化)
首先声明,本博文部分内容仅仅适用于ACM竞赛,并不适用于NOIP与OI竞赛,违规使用可能会遭竞赛处理,请慎重使用!遭遇任何情况都与本人无关哈=7= 我也不想搞得那么严肃的,但真的有些函数在NOIP与O ...
- 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)
链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...
随机推荐
- Spring Cloud学习笔记-002
搭建Spring Cloud注册中心:Eureka 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号.版本号.通信协议等一些附加信息告诉注 ...
- CodeForces 912d fishes(优先队列+期望)
While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of ...
- 关于手机APP开发的一些常见误解
移动应用不适用于小型企业和创业公司 如果你想要开发一款移动APP,那么在前期开发一种偏见的态度并不是一个好主意.如果你认为移动APP只有跨国公司和大企业创业者才会去开发,那么你可能错过了成功的捷径.而 ...
- Linux之centos系统常用命令总结
1.查看系统版本 cat /etc/redhat-release 文件与目录操作 命令 解析 cd /home 进入 '/home' 目录 cd .. 返回上一级目录 cd ../.. 返回上两级目录 ...
- webpack构建react项目(一)
前言 下面是我们使用到技术栈: webpack + react + redux + react-router + react-thunk + ES6 + .... 注意事项: 建议使用npm5.X 或 ...
- windows版本的phantomjs-2.1.1-windows安装
windows版本的phantomjs-2.1.1-windows安装 1.下载 链接:http://pan.baidu.com/s/1dEUl6dN 密码:oij8 2.安装 下载好之后解压放到某个 ...
- Java操作属性文件与国际化
在前面讲到的java类集中的Hashtable中,有一个子类Properties,此类定义如下: public class Properties extends Hashtable<Object ...
- Mac 下安装 MySQL 经历
1.使用 homebrew 安装: brew install mysql 结果报错: $ brew install mysql ==> Downloading http://dev.mysql. ...
- [SCOI2009]围豆豆
Description Input 第一行两个整数N和M,为矩阵的边长. 第二行一个整数D,为豆子的总个数. 第三行包含D个整数V1到VD,分别为每颗豆子的分值. 接着N行有一个N×M的字符矩阵来描述 ...
- [Codeforces]856D - Masha and Cactus
题目大意:给出一棵树和若干条可以加入的边,要求加入若干条边使图是仙人掌并且加入的边权和最大,仙人掌定义为没有一个点属于超过1个环.(n,m<=200,000) 做法:这题的仙人掌跟平时见到的不太 ...