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 ...
随机推荐
- K:大数加法
相关介绍: 在java中,整数是有最大上限的.所谓大数是指超过整数最大上限的数,例如18 452 543 389 943 209 789 324 233和8 123 534 323 432 323 ...
- 用css动画写一个下红包雨的效果
红包雨的功能相信大家都做过,不过一般都是用js计算的,闲着无聊用css的样式写了类似的,主要用的是css的transform和animation结合.大概代码逻辑: @keyframes startH ...
- js中windows的函数(随机数,计时器的实现)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Freebsd10.3 Nginx多版本PHP
服务器上需要使用多个php版本,费了好几天劲,终于有所收获.记录如下: 1.下载php-5.5.37.tar.bz2. 2.tar zvxf php-5.5.37.tar.bz2 -C /usr/l ...
- 转:使用VS Code断点调试PHP
使用VS Code断点调试PHP vs code 使用一款杰出的轻量级代码编辑器,其中的插件工具不胜枚举而且还在不断增加.使用 vs code 调试 PHP 代码更是方便简洁,下面我们来一起看一下. ...
- How to save rules of the iptables?
The easy way is to use iptables-persistent. Install iptables-persistent: sudo apt-get install iptabl ...
- C#调用SQlite常见问题汇总
最近在做SQLite开发,开发环境是VS2010+ SQLite Ado.Net data Provider.这套Data Provider程序是基于System.Data.SQLite 1.0.66 ...
- 小程序-报错 xxx is not defined (已解决)
小程序-报错 xxx is not defined (已解决) 问题情境: 这样一段代码,微信的小程序报错 is not defined 我 wxml 想这样调用 //wxml 代码 <view ...
- Android 线程池的类型、区别以及为何要用线程池
每个 Android 应用进程在创建时,会同时创建一个线程,我们称之为主线程,负责更新 UI 界面以及和处理用户之间的交互,因此,在 Android 中,我们又称之为 UI 线程.一个进程中 UI 线 ...
- Linux 虚拟机配置-network is unreachable
配置虚拟机时,遇到network is unreachable,根据网上找来的方法处理,最终自己试过,成功修改的方法在这里记录一下: 修改虚拟机的网络适配器:桥接,复制物理机网络 vim /etc/s ...