poj1703--Find them, Catch them(并查集应用)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 32073 | Accepted: 9890 |
Description
The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)
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
as described above.
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.
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 110000
int c[maxn] , d[maxn] ;
int find1(int x)
{
if( c[x] != x )
{
c[x] = find1(c[x]) ;
d[x] = d[ c[x] ] ;
}
return c[x] ;
}
int main()
{
int t , n , m , i , j ;
char str[10] ;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &n, &m);
for(i = 1 ; i <= n ; i++)
c[i] = i ;
memset(d,-1,sizeof(d));
while(m--)
{
int a , b , x , y , xx , yy ;
scanf("%s %d %d", str, &a, &b);
x = find1(a) ;
y = find1(b) ;
if( str[0] == 'D' )
{
if(d[x] == -1 && d[y] == -1)
{
d[a] = b ; d[b] = a ;
}
else
{
if( d[x] != -1 )
{
if( d[y] != -1 )
{
xx = d[y] ;
xx = find1(xx) ;
c[xx] = x ;
d[xx] = d[x] ;
}
c[y] = d[x] ;
d[y] = x ; }
else
{
if( d[x] != -1 )
{
yy = d[x] ;
yy = find1(yy) ;
c[yy] = y ;
d[yy] = d[y] ;
}
c[x] = d[y] ;
d[x] = y ;
}
}
}
else
{
if( x == y )
printf("In the same gang.\n");
else if( d[x] == -1 || d[y] == -1 || d[x] != y || d[y] != x )
printf("Not sure yet.\n");
else if( d[x] == y || d[y] != x )
printf("In different gangs.\n"); }
}
}
return 0;
}
poj1703--Find them, Catch them(并查集应用)的更多相关文章
- poj1703 Find them, Catch them 并查集
poj(1703) Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26992 ...
- POJ-1703 Find them, Catch them(并查集&数组记录状态)
题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- POJ1703-Find them, Catch them 并查集构造
Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...
- 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 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- poj1703_Find them, Catch them_并查集
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42451 Accepted: ...
- poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
随机推荐
- WP系统推广难的原因之中的一个之我见
个人也觉得如今的Android市场几家独大,竞争太激烈.利润空间挤压太严重,有实力的厂家不如尝试剑走偏锋,在其它大佬都还没跟进的时候,把市场投向WP.先入为主,不失为良策! 话说Microsoft不开 ...
- [RxJS] Basic DOM Rendering with Subscribe
While frameworks like Angular 2 and CycleJS provides great ways to update the DOM and handle subscri ...
- SuperSocket学习笔记(二)
上一篇博客SuperSocket学习笔记(一)说明了怎么快速搭建一个服务器端,这篇文章我想深挖一下SuperSocket 1. 每一个客户端连接到服务器端时,服务器端会将客户端的信息保存到一个Sess ...
- web.config中的profile
aspnet_regsql命令创建需要的表结构 public class UserProfile:ProfileBase { [SettingsAllowAnonymous(true)] //默认匿名 ...
- oracle常用自定义函数集合
1.Oracle 判断值是否为数字的函数CREATE OR REPLACE FUNCTION ISNUMBER(MyStr VARCHAR2) RETURN NUMBERIS STR VARCHAR ...
- C#如何设置下拉COMMBOX为不可输入,只有下拉条目
设置下拉框的DropDownStyle属性为DropDownList
- show,hide与fadeIn、fadeOu的区别
show,hide——>是通过改变height,width,opacity来实现动画的显示与隐藏的 fadeIn.fadeOut——>只通过opacity来实现动画的显示与隐藏的 它们两个 ...
- Hibernate配置文件中映射元素详解
对象标识符号 在关系数据库表中,主键(Primary Key)用来识别记录,并保证每条记录的唯一性.在Java语言中,通过比较两个变量所引用对象的内存地址是否相同,或者比较两个变量引用的对象值是否相同 ...
- Python函数小结(2)-- 装饰器、 lambda
本篇依然是一篇学习笔记,文章的结构首先讲装饰器,然后讲lambda表达式.装饰器内容较多,先简要介绍了装饰器语法,之后详细介绍理解和使用不带参数装饰器时应当注意到的一些细节,然后实现了一个简单的缓存装 ...
- JavaWeb学习笔记--Servlet代码集
目录: 登录系统提交表单数据打开PDFCookieURL传递参数URL重写跟踪会话使用HttpSession对象跟踪会话Servlet间协作过滤器Filter 登录系统 <!DOCTYPE HT ...