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 ...
随机推荐
- POJ-3292 Semi-prime H-numbers---筛素数
题目链接: https://vjudge.net/problem/POJ-3292 题目大意: 定义一种数叫H-numbers,它是所有能除以四余一的数. 在H-numbers中分三种数: 1.H-p ...
- html标记语言 --文本标记
html标记语言 --文本标记 二.文本标记 1.h1-h6 标题标记,h1最大 2.font 字体设置标记 2.1 size字体大小.<font size="> 取值范围1-7 ...
- 分析ajax请求抓取今日头条关键字美图
# 目标:抓取今日头条关键字美图 # 思路: # 一.分析目标站点 # 二.构造ajax请求,用requests请求到索引页的内容,正则+BeautifulSoup得到索引url # 三.对索引url ...
- JavaWeb小项目(一)
总结一下前段时间,在学了JSP.Servlet.JavaBean后,配合Tomcat服务器加上MySQl数据库搭的第一个简单网站. 前前后后,在学习了以上说的这些概念知识后,还进一步熟悉了整个搭建的流 ...
- [LeetCode] Smallest Range 最小的范围
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- 使用springcloud zuul构建接口网关
一 微服务网关背景及简介 不同的微服务一般有不同的网络地址,而外部的客户端可能需要调用多个服务的接口才能完成一个业务需求.比如一个电影购票的收集APP,可能回调用电影分类微服务,用户微服务,支付微服 ...
- servlet之重写
package app02a;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;impo ...
- windows版本的phantomjs-2.1.1-windows安装
windows版本的phantomjs-2.1.1-windows安装 1.下载 链接:http://pan.baidu.com/s/1dEUl6dN 密码:oij8 2.安装 下载好之后解压放到某个 ...
- [BZOJ 3332]旧试题
Description 圣诞节将至.一年一度的难题又摆在wyx面前——如何给妹纸送礼物. wyx的后宫有n人,这n人之间有着复杂的关系网,相互认识的人有m对.wyx想要量化后宫之间的亲密度,于是准备给 ...
- 【NOIP 2017】逛公园
Description 策策同学特别喜欢逛公园.公园可以看成一张N个点M条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,N号点是公园的出口,每条边有一个非负权值, 代表策策经过这条边所要 ...