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 ...
随机推荐
- hashlib 文件校验,MD5动态加盐返回加密后字符
hashlib 文件校验 # for循环校验 import hashlib def check_md5(file): ret = hashlib.md5() with open(file, mode= ...
- Node.js如何找npm模板
首先需要去官网下载npm文件 https://www.npmjs.com/ 下载完成,使用CD查看是否安装完成 然后就是贴代码看npm模板的功能 var _ = require('underscore ...
- Django,ajax实现表格增删查改,Django内置分页功能。
1.工程目录 2.urls.py """Django_ajax URL Configuration The `urlpatterns` list routes URLs ...
- VScode基础设置
安装依赖包: • One Monokai • Aglia • One Dark Pro • Material Icon 漂亮的主题: Themes Quokka 是一个调试工具插件,能够根据你正在 ...
- C++学习笔记(6)----基类和派生类的构造函数和析构函数的执行顺序
基类和派生类:构造函数和析构函数的执行顺序 在Visual Studio中,新建控制台工程,构造类如下: #include<iostream> using namespace std; c ...
- Volley框架实现Http的get和post请求
一: volley简介: Google I/O 2013上,Volley发布了.Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮.这是Volley名称的由来: a bu ...
- 安装 Python IDLE (Linux)
Python IDLE (Integrated Development and Learning Environment) 是一个官方的轻量级 Python IDE.在不同的 Linux 的发布版本中 ...
- 转载:VMWARE虚拟机无法访问的三种方法分析
bridged(桥接模式).NAT(网络地址转换模式)host-only(主机模式).理论认识:1.bridged(桥接模式)在这个地方模式.虚拟机等同于网络内的一台物理主机,可对手动设置IP,子网掩 ...
- python源码学习(一)——python的总体架构
python源码学习(一)——python的总体架构 学习环境: 系统:ubuntu 12.04 STLpython版本:2.7既然要学习python的源码,首先我们要在电脑上安装python并且下载 ...
- ZT 王国维先生“人生三大境界”的具体含义是什么?
昨夜西风凋碧树.独上高楼,望尽天涯路. 衣带渐宽终不悔,为伊消得人憔悴. 众里寻他千百度,蓦然回首,那人却在,灯火阑珊处. 这三句本来都是言情话相思的佳句,却被王国维用以表现“悬思——苦索——顿悟”的 ...