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: ...
随机推荐
- HDU 2058 The sum problem
传送门 Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequen ...
- Java 序列化Serializable接口
1 什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是一种将这些字节重建成一个对象的过程. 2 什么情况下需要 ...
- 修改MySQL的默认数据存储引擎
因为MySQL默认的是MyISAM数据引擎,不支持事务也不支持外键,所以需要用到Innodb引擎,于是决定将mysql的默认引擎设置为innodb.1 . 查看MySQL存储引擎是用的哪个?登录MyS ...
- JS-Date对象
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>D ...
- vmware的安装
VMware Workstation是一款功能强大的虚拟机软件,在不影响本机操作系统的情况下,用户可以在虚拟机中同时运行不同版本的操作系统,用于开发.测试以及部署工作,这边小编为大家分享最新版本VMw ...
- 未签名有元程序集 Unsigned Friend Assemblies
C#中的访问修饰符internal可以使类型在同程序集中可以被相互访问.但有时会有这样一个要求,我们希望一个程序集中的类型可以被外部的某些程序集访问,如果设置成public的话,就被所有的外部程序集访 ...
- Spring MVC学习笔记——注解式控制器
- 收缩菜单 css变样
// 收缩菜单 $("#leftMenu li h3").click(function(){ $v = $(this).next('.ajax').css('display'); ...
- VS2013-解决error C4996: 'fopen'问题
VS2013中如何解决error C4996: 'fopen'问题 初次使用vs系列编辑器编写控制台应用程序时常出现如下错误: error C4996: 'fopen': This function ...
- centos 7.0 ssh 登陆
CentOS 7.0 最小化安装 默认连接 默认端口是22 安装完查看IP 命令 ip addr ip: 192.168.1.103 自己家里练习的机子 都没改配置了 使用putty 远程连接 下载页 ...