POJ 1703 Find them, Catch them(种类并查集)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 41463 | Accepted: 12753 |
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
题目链接:POJ 1703
主要思路:题目跟食物链不一样,都是真话,也不会存在前后矛盾的情况,那么只要考虑题目中说的两种情况;
1、A a b,问你a与b的的关系,那显然分成两种情况考虑
a与b的根不同即无关(根都不同了都不在一个集合内肯定无关)
a与b根相同即在一个集合内(有关系),用公式求关系值判断这俩的帮派是1(不同)还是0(相同)
2、D a b,指定了a与b不同帮派即这俩关系之间的值要用1代入,那合并的时候a->b的值用1代即可,然后就可以水过了
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
struct info
{
int rela;
int pre;
};
info node[N];
void init(int n)
{
for (int i=0; i<=n; ++i)
{
node[i].rela=0;
node[i].pre=i;
}
}
int find(int n)
{
if(node[n].pre==n)
return n;
int tpre=node[n].pre;
node[n].pre=find(node[n].pre);
node[n].rela=(node[n].rela+node[tpre].rela)%2;
return node[n].pre;
}
void joint(int a,int b)
{
int fa=find(a),fb=find(b);
node[fb].pre=fa;
node[fb].rela=(node[a].rela+1-node[b].rela+2)%2;
}
int same(int a,int b)
{
int fa=find(a),fb=find(b);
if(fa==fb)
return (node[a].rela==node[b].rela);
else
return -1;
}
int main(void)
{
int tcase,i;
int n,m,a,b;
char ops[3];
scanf("%d",&tcase);
while (tcase--)
{
scanf("%d%d",&n,&m);
init(n);
for (i=0; i<m; ++i)
{
scanf("%s%d%d",ops,&a,&b);
if(ops[0]=='A')
{
switch(same(a,b))
{
case 0:puts("In different gangs.");break;
case 1:puts("In the same gang.");break;
case -1:puts("Not sure yet.");break;
}
}
else
joint(a,b);
}
}
return 0;
}
POJ 1703 Find them, Catch them(种类并查集)的更多相关文章
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- 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(并查集)
题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...
- POJ 1703 Find them, Catch them (并查集)
题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派. 思路: 解法一: 编号划分 定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素 ...
- POJ 1703 Find them, Catch them(并查集拓展)
Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...
- POJ 1703 Find them, Catch them(并查集,等价关系)
DisjointSet保存的是等价关系,对于某个人X,设置两个变量Xa,Xb.Xa表示X属于a帮派,Xb类似. 如果X和Y不是同一个帮派,那么Xa -> Yb,Yb -> Xa... (X ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- POJ:1703-Find them, Catch them(并查集好题)(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49867 Accepted: 153 ...
随机推荐
- 获取4G以上的文件大小
1.DWORD dwFileSizeHigh; // 得到文件大小的高位 __int64 qwFileSize = GetFileSize(m_hSrcBigFile, &dwFileSi ...
- CUDA学习笔记(三)——CUDA内存
转自:http://blog.sina.com.cn/s/blog_48b9e1f90100fm5f.html 结合lec07_intro_cuda.pptx学习 内存类型 CGMA: Compute ...
- Linux用户名显示-bash-4.1$快速排查
最近项目使用的的服务器有点多(100多台),很多开发同事经常问这个问题,现在整理如下: 几个可能导致的原因: 1 用户的家目录所属组被改为root,解决方法使用root执行cd /home/;chow ...
- Java之IO操作总结
所谓IO,也就是Input与Output的缩写.在java中,IO涉及的范围比较大,这里主要讨论针对文件内容的读写 其他知识点将放置后续章节 对于文件内容的操作主要分为两大类 分别是: 字符流 字节流 ...
- php 条件查询和多条件查询
条件循环 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- ***微信公众平台开发: 获取用户基本信息+OAuth2.0网页授权
本文介绍如何获得微信公众平台关注用户的基本信息,包括昵称.头像.性别.国家.省份.城市.语言.本文的方法将囊括订阅号和服务号以及自定义菜单各种场景,无论是否有高级接口权限,都有办法来获得用户基本信息, ...
- ☆☆配置NDK环境
1 前提是 已经配置好 安卓SDK开发环境. 2 下载 android-ndk64-r10-windows-x86_64,可以从官方网站下载,这里有一个现成的. http://pan.baidu.co ...
- 电赛总结(二)——AD之STM32F102ZE单片机自带12位AD
直接上程序即可 #ifndef __ADC_H #define __ADC_H #include "stm32f10x.h" #include "LCD3.2.h&quo ...
- SQL Server--用户自定义函数
除了使用系统提供的函数外,用户还可以根据需要自定义函数.用户自定义函数是 SQL Server 2000 新增的数据库对象,是 SQL Server 的一大改进.与编程语言中的函数类似,Microso ...
- Listview点击事件
listview = (ListView) findViewById(R.id.listview); // 填充data数据 data = new ArrayList<String>(); ...