【POJ - 1703】Find them, Catch them(种类并查集)
Find them, Catch them
直接翻译了
Descriptions
D a b 表示a、b是不同帮派
A a b 询问a、b关系
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.
题目链接
https://vjudge.net/problem/POJ-1703
定义并查集为:
并查集里的元素i-x表示i属于帮派x
同一个并查集的元素同时成立
可见所有元素个数为2 * N,如果i表示属于帮派A,那么i + N表示属于帮派B,每次输入两个家伙不在同一帮派的时候,就合并他们分属两个帮派的元素。
注意这题如果用cin的话会TLE
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 100000 * 2 + 10
using namespace std;
int T,N,M;
int par[Maxn];//par[i] i的根
void init(int n)
{
for(int i=; i<=n; i++)
par[i]=i;
}
int findr(int x)//查询根
{
if(par[x]==x)
return x;
return par[x]=findr(par[x]);
}
void unite(int x,int y)//合并
{
x=findr(x);
y=findr(y);
if(x==y)//根相同不用管
return;
par[x]=y;//若根不同,y并入x,则y的根为x,同理x也能并入y 这里随意
}
bool same(int x,int y)//x和y是否在一个集合
{
return findr(x)==findr(y);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
init(N*);
char op[];
int a,b;
for(int i=; i<M; i++)
{
scanf("%s%d%d", op, &a, &b);
if(op[]=='A')
{
if(same(a,b))
printf("In the same gang.\n");
else if(same(a,b+N))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
else
{
unite(a+N,b);
unite(a,b+N);
}
}
}
return ;
}
【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 ...
随机推荐
- C# 运算符和类型强制转换(6) 持续更新
C#支持的运算符 https://msdn.microsoft.com/zh-cn/library/6a71f45d(v=vs.140).aspx checked 和 unchecked ; b++; ...
- java接口的多继承
Java类之间并不允许多继承,只可以单继承和实现多接口,一直以为接口也是一样的,但是查阅了相关资料,突然豁然开朗. 一个类只能extends一个父类,但可以implements多个接口. 一个接口则可 ...
- uni-app引入css动画库
引入Animate动画库 Animate中文网地址:http://www.animate.net.cn/ Animate下载地址:https://daneden.github.io/animate.c ...
- Laravel Passport API 认证使用小结
Laravel Passport API 认证使用小结 八月 4, 2017 发布在 Laravel 看到Laravel-China 社区常有人问 Laravel Passport 用于密码验证方式来 ...
- UnhandledExceptionPolicy
winform程序未捕获异常解决方法 EventType clr20r3 P1 http://blog.csdn.net/chichaodechao/article/details/8294922
- node中fs内置模块
Node.js内置的fs模块就是文件系统模块,负责读写文件. 和所有其它JavaScript模块不同的是,fs模块同时提供了异步和同步的方法. 回顾一下什么是异步方法.因为JavaScript的单线程 ...
- [linux]kali apt-get 安装mysql
环境: Linux kali 4.18.0-kali2-amd64 #1 SMP Debian 4.18.10-2kali1 (2018-10-09) x86_64 GNU/Linux 本来是想要装m ...
- 如何用20行Python代码打造一个微信群聊助手?
今天要教大家一个黑科技,20行代码实现自己定制的微信群聊助手,可以用来活跃群气氛,好多群主创建完群后,拉完一群人,之后就一片寂静,有个群聊助手,就可以帮忙活跃群里气氛,通过今天在自己的微信上有一大批好 ...
- flask 第九篇 蓝图 Blueprint
蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看 ...
- Oracle用户被锁解决方法
.查看用户的proifle是哪个,一般是default: sql>SELECT username,PROFILE FROM dba_users; .查看指定概要文件(如default)的密码有效 ...