POJ 1703 Find them, Catch them(确定元素归属集合的并查集)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 52925 | Accepted: 16209 |
Description
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.
Source
union_set(x, y)同属于第一个团伙
union_set(x+n,y+n)同属于第二个团伙
union_set(x+n, y)表示x属于第二个团伙,y属于第一个团伙
union_set(x, y+n)表示x属于第一个团伙,y属于第二个团伙;
每次告诉你x和y属于不同团伙,有两种情况,x属于1,y属于2
x属于2,y属于1,所以需要这样进行两次合并
确定是否属于相同团伙的话
x与y的根结点相同或者x+n与y+n的根结点相同 都属于相同犯罪团伙
x+n与y根结点相同或者x与y+n的根结点相同,都属于不同犯罪团伙
其他情况就不能确定了
这个思路很巧妙,也比较简单,不用具体确定x属于1还是2
code:
#include<queue>
#include<set>
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define max_v 100005
int pa[max_v*];
int rk[max_v*];
int n,m;
void init()
{
for(int i=; i<=*n; i++)
{
pa[i]=i;
rk[i]=;
}
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(x==y)
return ;
if(rk[x]>rk[y])
pa[y]=x;
else
{
pa[x]=y;
if(rk[x]==rk[y])
rk[y]++;
}
}
int f(int x,int y)
{
return find_set(x)==find_set(y);
}
int main()
{
int t;
scanf("%d",&t);
int x,y;
char str[];
while(t--)
{
scanf("%d %d",&n,&m);
init(); for(int i=; i<m; i++)
{
scanf("%s %d %d",str,&x,&y);
if(str[]=='D')
{
union_set(x+n,y);
union_set(x,y+n);
}
else if(str[]=='A')
{
if(f(x,y)||f(x+n,y+n))
printf("In the same gang.\n");
else if(f(x+n,y)||f(x,y+n))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
}
}
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【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】
The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...
- 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(种类并查集)
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 【并查集 新写法的思路】
题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...
- poj 1703 Find them, Catch them(种类并查集和一种巧妙的方法)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36176 Accepted: ...
- 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(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
随机推荐
- spss C# 二次开发 学习笔记(四)——Spss授权
Spss的授权方式有两种,单机版和网络版. Spss的激活,在联网的情况下,通过20位的激活码激活,在未联网的情况下,Spss根据机器获取一个类似4-XXXX的锁定码,然后由激活码和锁定码算出一个授权 ...
- Spring Data JPA —— 快速入门
一.概述 JPA : Java Persistence API, Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. Spring D ...
- 创建Web项目运行时出小错误及解决方法
1.目录结构 2.各文件内容 index.jsp <%@ page contentType="text/html;charset=UTF-8" language=" ...
- Class.forName("com.mysql.jdbc.Driver")找不到类
解决方法: 如果是java项目,只需要引入mysql-connector-java-8.0.13.jar就可以运行java项目. 建的如果是web工程,需要把mysql-connector-java- ...
- input textarea监听鼠标粘贴
发现一个问题,在input/textarea中如果是鼠标粘贴内容进去,发现判断不了value的改变,html代码如下: <!doctype html> <html> <h ...
- 创建一个Dribbble的作品展示
Most designers on dribbble have a personal portfolio website that usually consists of a name and a b ...
- PHP学习笔记(二) ---- PHP数据类型
PHP __数据结构类型 一.php 中的八种数据类型 1.四种标量类型 Boolean (布尔类型): true or false,多用于条件判断. 实例: <?php $x = &qu ...
- Canvas中的剪切clip()方法
Canvas中的剪切 接下来我们要聊的不是图像的合成,而是Canvas中的另一个有用的功能:剪切区域.它是Canvas之中由路径所定义的一块区域,浏览器会将所有的绘图操作都限制在本区域内执行.在默认情 ...
- Linux菜鸟简单命令
想要使用Linux,以下这些命令不可少的哦! 我在工作中经常用到的大多数都是一些文件的查找,和上传下载什么的,没什么技术含量,所以除了自己整理的之外,还有借鉴的别的大神的一些命令,我会在最后标注的\( ...
- JQ的几组API辨析:
1.插入: Jq的插入一共有八个API可供选择,结果相类似,下面将以例子,简单明了的介绍下用法: <select name="one" multiple="mult ...