poj.1703.Find them, Catch them(并查集)
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
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.
#include<stdio.h>
#include<set>
#include<map>
#include<algorithm>
#include<string.h>
const int M = 1e5 + ;
int T ;
int n , m ;
int f[M] ;
int mm[M] ;
int Union (int x)
{
return f[x] == x ? x : f[x] = Union (f[x]) ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
scanf ("%d" , &T) ;
while (T --) {
scanf ("%d%d" , &n , &m);
memset (mm , , sizeof(mm)) ;
for (int i = ; i <= n ; i ++) f[i] = i ;
char s[] ;
int _u , _v , u , v ;
int x , y ;
int _x , _y ;
while (m --) {
scanf ("%s" , s) ;
if (s[] == 'D') {
scanf ("%d%d" , &u , &v ) ;
if (mm[u] < mm[v]) std::swap (u , v) ;
if (mm[u] && mm[v]) {
_u = mm[u] ; _v = mm[v] ;
x = Union (_u) ; y = Union (v) ;
f[y] = x ;
x = Union (_v) ; y = Union (u) ;
f[y] = x ;
}
else if (mm[u] && mm[v] == ) {
_u = mm[u] ;
x = Union (_u) ; y = Union (v) ;
f[y] = x ;
mm[v] = u ;
}
else if (mm[u] == ) {
mm[u] = v ;
mm[v] = u ;
}
}
else if (s[] == 'A') {
scanf ("%d%d" , &u , &v) ;
if (mm[u] < mm[v]) std::swap (u , v) ;
if ( !mm[u] || !mm[v]) puts ("Not sure yet.") ;
else {
x = Union (u) ; y = Union (v) ;
if (x == y) puts ("In the same gang.") ;
else {
_x = Union (mm[u]) ;
if (_x != y) puts ("Not sure yet.") ;
else puts ("In different gangs.") ;
}
}
}
}
}
return ;
}
poj.1703.Find them, Catch them(并查集)的更多相关文章
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
- POJ 1703 Find them, Catch them 并查集,还是有点不理解
题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
随机推荐
- 电影发烧友必备知识-720P、1080P、4K的区别
随着技术的进步,现在的影视作品的清晰度也越来越高,观众的体验也越来越好,普清的电影基本没人看了,尤其是影视爱好者现在都是看1080P或蓝光原盘.4K. 目前主流清晰度主要分为720P(高清).1080 ...
- iOS - 果冻效果
具体使用的CADisplayLink和贝塞尔曲线 下载地址:https://github.com/nLoser/CustomAnimation 效果: // // DisplayView.m // C ...
- js003-基本概念
js003-基本概念 3.1 语法 3.1.1 区分大小写 ECMAScript中的一切(变量.函数名和操作符)都是区分大小写的,并且不能用关键字作为函数名:如 typeof. 3.1.2 标识符 所 ...
- re正则表达式13_review of regex symbols
Review of Regex Symbols This chapter covered a lot of notation, so here’s a quick review of what you ...
- 谈谈我对PhoneGap的看法——(摘自唐巧的技术博客)
源地址:http://blog.devtang.com/blog/2012/03/24/talk-about-uiwebview-and-phonegap/ 主题部分 我认为PhoneGap有以下3大 ...
- Yii2 高级版新建一个 Api 应用
原文地址:http://www.getyii.com/topic/28 先在项目的根目录下复制一份 backend 为 api: cp backend/ api -r 拷贝 api 环境 cp -a ...
- 浅谈JavaScript原型对象与相关设计模式
引言 本文先从介绍JavaScript中基本的几种设计模式开始,最后引出原型对象,然后对原型对象有一个较全面的介绍. 1.创建对象的几种设计模式 A.工厂模式 我们知道在JavaScript中创建对象 ...
- C# 正则匹配domain
1.带协议表达式 var pattern = @"[(?<=http://)|(?<=https://)]+[\w\.]+[^/?#]"; 2.不带协议表达式 var ...
- 在github上创建新分支
在github上创建仓库: Create a new repository on the command line touch README.md git init git add README.md ...
- Mysql函数:Last_insert_id()语法讲解
Mysql函数可以实现许多我们需要的功能,下面介绍的Mysql函数Last_insert_id()就是其中之一,希望对您学习Mysql函数能有所帮助. 自动返回最后一个INSERT或 UPDATE 查 ...