POJ1703(2集合并查集)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 39402 | Accepted: 12101 |
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.
/*
@ title : poj1703
@ method : union-find sets
@ result : Accepted 1948K 813MS G++ 967B
@ auther : baneHunter
@ date : 2016/3/19
*/
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int par[MAXN+MAXN],rnk[MAXN+MAXN];
int n,m;
void prep()
{
for(int i=;i<MAXN+MAXN;i++)
{
par[i]=i;
rnk[i]=;
}
} int fnd(int x)
{
if(x==par[x])
return x;
return par[x]=fnd(par[x]);
} void unite(int x,int y)
{
int a=fnd(x);
int b=fnd(y);
if(a==b) return ;
if(rnk[a]<rnk[b])
{
par[a]=b;
}
else
{
par[b]=a;
if(rnk[a]==rnk[b])
rnk[a]++;
}
} bool same(int x,int y)
{
return fnd(x)==fnd(y);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
prep();
while(m--)
{
scanf("%*c");
char op;
int x,y;
scanf("%c",&op);
scanf("%d%d",&x,&y);
if(op=='A')
{
if(same(x,y))
{
printf("In the same gang.\n");
}
else if(same(x,y+n))
{
printf("In different gangs.\n");
}
else printf("Not sure yet.\n");
}
else
{
unite(x+n,y);
unite(x,y+n);
}
}
} return ;
}
POJ1703(2集合并查集)的更多相关文章
- 51NOD 1821 最优集合 [并查集]
传送门 题意: 一个集合S的优美值定义为:最大的x,满足对于任意i∈[1,x],都存在一个S的子集S',使得S'中元素之和为i. 给定n个集合,对于每一次询问,指定一个集合S1和一个集合S2,以及一个 ...
- 【并查集】 不相交集合 - 并查集 教程(文章作者:Slyar)
最近写了一个多星期的并查集,一瞬间贴出这么多解题报告,我想关于并查集的应用先告一段落吧,先总结一下. 在网上看到一篇关于并查集比较好的教程(姑且允许我这么说吧),不转过来是在可惜.献给爱学习的你 文章 ...
- poj1703(种类并查集)
题意:有两个犯罪集团,现在有两种操作,D [a] [b]表示a和b是属于不同犯罪集团的,A [a] [b] 是询问你a和b的关系,如果ab属于同一个犯罪集团,输出In the same gang. ...
- POJ1703带权并查集(距离或者异或)
题意: 有两个黑社会帮派,有n个人,他们肯定属于两个帮派中的一个,然后有两种操作 1 D a b 给出a b 两个人不属于同一个帮派 2 A a b 问a b 两个人关系 输出 同一个帮派 ...
- 并查集(HDOJ 1856)
并查集 英文:Disjoint Set,即“不相交集合” 将编号分别为1…N的N个对象划分为不相交集合, 在每个集合中,选择其中某个元素代表所在集合. 常见两种操作: n 合并两个集合 ...
- 并查集 (Union-Find Sets)及其应用
定义 并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.常常在使用中以森林来表示. 集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的 ...
- poj 1611 The Suspects(第一道并查集)
题意: 有N个学生,编号为0-n-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染, 并且这些人如果还参加了别的社团,他所在的社团照样全部感染,社团个数为m,求感染的人数. 输入: n代表人 ...
- hdu 1856 More is better (并查集)
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- C. K-Complete Word(小小的并查集啦~)
永久打开的传送门 \(\color{Pink}{-------------分割-------------}\) \(n最大有2e5,那么暴力一定不行,找规律\) \(我们发现第i位的字符一定和第i+k ...
随机推荐
- htop的使用
htop是top的增强版本.官网地址: http://hisham.hm/htop/ 这网站比较...... 实验环境: [root@miyan ~]# cat /etc/redhat-release ...
- 牛客练习赛13 A 幸运数字Ⅰ 【暴力】
题目链接 https://www.nowcoder.com/acm/contest/70/A 思路 暴力每一个子串 用 MAP 标记一下 然后 最后 遍历一遍 MAP 找出 出现次数最多 并且 字典序 ...
- Android SDK上手指南 2:用户界面设计
http://mobile.51cto.com/ahot-419184.htm 内容简介 我们将为应用程序项目添加布局方案,在这方面XML与Eclipse ADT接口将成为工作中的得力助手——不过在后 ...
- Executor中的类
Executor框架 其中ThreadPoolExecutor非常重要,通过这个类自定义线程池 public ThreadPoolExecutor(int corePoolSize, //线程池里面的 ...
- echo 命令参数
echo 命令参数: -n 不换行输出 -e 解析转义字符(\n \t \b \r)
- Monkey for Mac 环境配置
Monkey for Mac环境配置步骤 java环境配置, 直接去官网找对应jdk就可以了 Android AdtBundle环境配置 1) 下载地址: http://www.jianshu.co ...
- [Vue] 初识Vue-常用指令
Vue Vue是一套用于构建用户界面的渐进式框架, Vue的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合.Vue是一个JavaScript MVVM库, 以数据驱动和组件化的思想构 ...
- java入门了解06
1.进程 : (一)正在执行的程序称作为一个进程. 进程负责了内存空间的划分. (二)问题: windows号称是多任务的操作系统,那么windows是同时运行多个应用程序吗? 从宏观的角度: ...
- 测试CPU核心个数
//测试CPU核心个数 #if !defined (_WIN32) && !defined (_WIN64) #define LINUX #include <unistd.h&g ...
- intel dpdk api interrupt module 中断模块介绍
声明:此文档只做学习交流使用,请勿用作其他商业用途 author:朝阳_tonyE-mail : linzhaolover@gmail.comCreate Date: 2013-7-12 11:46: ...