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: ...
随机推荐
- Newton-Raphson算法简介及其R实现
本文简要介绍了Newton-Raphson方法及其R语言实现并给出几道练习题供参考使用. 下载PDF格式文档(Academia.edu) Newton-Raphson Method Let $f(x) ...
- django redis操作
from utils.redis.connect import redis_cache as rr.flushdb() 列表操作 r.lpush("name", xxxx) or ...
- Java反射API使用实例
/** * 访问Class对应的类所包含的注释:getAnnotation();getDelaredAnnotation(); * 访问Class对应的类所包含的内部类:getDecl ...
- 【Beta版本】冲刺-Day5
队伍:606notconnected 会议时间:12月13日 目录 一.行与思 二.站立式会议图片 三.燃尽图 四.代码Check-in 一.行与思 张斯巍(433) 今日进展:继续修改界面及图标设计 ...
- Android 使用 DownloadManager 管理系统下载任务的方法,android管理系统
从Android 2.3(API level 9)开始Android用系统服务(Service)的方式提供了Download Manager来优化处理长时间的下载操作.Download Manager ...
- SVN cleanup操作反复失败解决办法
今天在更新项目的时候遇到一个问题,按惯例要cleanup才能重新更新.但是很不幸,在cleanup的时候又遇到了问题! 1 svn cleanup failed–previous operati ...
- bootstrap标签引入地址
http://www.bootcdn.cn/bootstrap/ <link rel="stylesheet" href="http://apps.bdimg.co ...
- CentOS6.5个人目录下中文路径转英文路径
如果安装了中文版到CentOS之后,root目录及home目录下会出现中文到路径名,如“桌面”.“文档”,“图片 .公共的” .“下载”. “音乐”.“ 视频”等目录,这样在命令行上操作十分到不方便. ...
- Scribe日志收集工具
Scribe日志收集工具 概述 Scribe是facebook开源的日志收集系统,在facebook内部已经得到大量的应用.它能够从各种日志源上收集日志,存储到一个中央存储系统(可以是NFS,分布式文 ...
- php实现发送邮件
smtp.php: <?php class smtp { /* Public Variables */ var $smtp_port; var $time_out; ...