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 ...
随机推荐
- HTML-----<a>、<table>、<form>解析
超链接 anchor 锚 <a href="url">内容</a> Href Hypertext reference 引用 URL(Uniform Re ...
- .net core 使用阿里云短信发送SMS
阿里云官方的skd(aliyun-net-sdk-core,aliyun-net-sdk-dysmsapi)在dnc中发送短信会出错,nuget上的包貌似也一样不管用.直接改下sdk当然也可以,但就发 ...
- vue2路由
我们在前面的学习过程中不管是在学习angular还是vue1,都会遇到二级路由,我们现在先来看一下vue2中的一级路由. 首先要引入的是vue2与路由文件. js代码: <script> ...
- vuex commit保存数据技巧
vuex 单向数据流,推荐的commit 改变state数据,写起来非常繁琐,因为改数据可能要写很多commit函数. 依据我的理解,单向数据流主要是为了避免数据混乱,便于调试. 说白了,就是一个数据 ...
- 【转载】C++基本功和 Design Pattern系列 ctor & dtor
最近实在是太忙了,无工夫写呀.只能慢慢来了.呵呵,今天Aear讲的是class.ctor 也就是constructor, 和 class.dtor, destructor. 相信大家都知道const ...
- Python3 面向对象编程之程序设计思想发展
概述 1940年以前:面向机器 1940年以前:面向机器 最早的程序设计都是采用机器语言来编写的,直接使用二进制码来表示机器能够识别和执行的指令和数 据.简单来说,就是直接编写 和 的序列来代表程序语 ...
- oracle11.2中分区功能测试之add&split partition对global&local index的影响
生产库中某些大表的分区异常,需要对现有表进行在线操作,以添加丢失分区,因为是生产库,还是谨慎点好,今天有空,针对add&split分区对global&local索引的影响进行了测试,测 ...
- 一起做orb-slam(2)
1.ushort用法? USHORT is a macro which is not part of the official C++ language (it's probably defined ...
- [LeetCode] Super Washing Machines 超级洗衣机
You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...
- react组件开发规范(一)
这是通过修改项目运行在Google上时的警告,总结的的部分react组件开发规范: (1)编写组件时,一定要写PropTypes,切莫为了省事儿而不写! 如果一个Props不是required,一定在 ...