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 ...
随机推荐
- 在IIS中部署好WCF服务站点后,本机访问服务无问题,局域网中其他电脑访问不到
1.问题描述 在IIS中部署好WCF服务站点后,本机访问服务无问题,局域网中其他电脑访问不到. 2.解决方法 (1)控制面板 -> Windows防火墙 -> 高级设置 (2)属性 (3) ...
- 工厂方法模式(GOF23)
耦合关系直接决定着软件面对变化时的行为 主要对模块之间的关系进行整理,依赖关系倒置(依赖反转),变化快的东西不能影响到变化慢的东西 用封装机制来隔离易变的对象,抽象部分(不易变)和细节部分(可能容易变 ...
- Ubuntu16.04下安装MATLAB 2016b!完美运行
目录 1.下载MATLAB 2016b 2.挂载iso 3.安装MATLAB 4.破解 5.快捷键与运行 1.下载MATLAB 2016b 下载matlab 2016b for linux(可以百度搜 ...
- js-script标签放在的位置
* 建议把script标签放到</body>后面 * 如果现在有这样一个需求 在js里面需要获取到input里面的值,如果把script标签放到head里面,会出现问题.HTML解析是从上 ...
- swiper移动端日历-1
先上图: 说明:这是基于移动端的,对于PC端浏览器支持不是很好(我测的结果是IE无效),另外这个swiper是4.x版本的 思路: 先引用css <link href="css/ ...
- GeoAnalytics Server学习笔记
GA的输入数据源 输入源 存储形式 Spatiotemporal 时空型ArcGIS DataStore 物联网数据 (通过GeoEvent Server输出) 大数据共享目录BigDataShare ...
- JAVA中获取文件的大小和文件的扩展名
一.获取文件扩展名(该段代码来自博客园网站装男人的博客https://www.cnblogs.com/nanrenzhuang/archive/2013/05/19/6315546.html) pub ...
- 新建虚拟机,每次都提示无法连接虚拟设备 ide1:0
处理方式:看到了这个老哥http://www.cnblogs.com/dean-du/p/6888513.html的博客,发现问题是一样的,所以记录一下. 将虚拟机设置中的CD/DVD选项中的连接更改 ...
- vue之computed(计算属性)
所谓计算属性就是计算data里的数据属性. computed:实时监听的该功能. 即监听是否有修改(浏览器未打开时即开始监听了),监听的值有修改则添加 所监听的data数据属性变化了,自动实时修改. ...
- centos下安装ipython(minglnghang命令行)
下载文件 wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate 执行安装 python get-pip.py 这就安装好了 ...