Find them, Catch them

直接翻译了

Descriptions

警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人。该城有N个罪犯,编号从1至N(N<=100000。将有M(M<=100000)次操作。
D a b 表示a、b是不同帮派
A a b 询问a、b关系


Input

多组数据。第一行是数据总数 T (1 <= T <= 20)每组数据第一行是N、M,接下来M行是操作


Output

对于每一个A操作,回答"In the same gang."或"In different gangs." 或"Not sure yet."


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(种类并查集)的更多相关文章

  1. POJ 1703 Find them,Catch them ----种类并查集(经典)

    http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...

  2. POJ 1703 Find them, Catch them(种类并查集)

    题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...

  3. 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 ...

  4. poj 1703 Find them, Catch them(并查集)

    题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...

  5. POJ 1703 Find them, Catch them (并查集)

    题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派. 思路: 解法一: 编号划分 定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素 ...

  6. 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 ...

  7. POJ 1703 Find them, Catch them(并查集,等价关系)

    DisjointSet保存的是等价关系,对于某个人X,设置两个变量Xa,Xb.Xa表示X属于a帮派,Xb类似. 如果X和Y不是同一个帮派,那么Xa -> Yb,Yb -> Xa... (X ...

  8. POJ1703Find them, Catch them[种类并查集]

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42416   Accepted: ...

  9. [poj1703]Find them, Catch them(种类并查集)

    题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...

  10. POJ:1703-Find them, Catch them(并查集好题)(种类并查集)

    Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49867 Accepted: 153 ...

随机推荐

  1. Oracle 11 安装 提示环境不满足最低要求解决方案

    在 Oracle 安装包 中,找到 stage 文件夹 切入,再找到 cvu 文件夹,找到 cvu_prereq.xml 文件 ,进行编辑 新增 这一块内容. <OPERATING_SYSTEM ...

  2. Java:应用Observer接口实践Observer模式

    本文出自“子 孑” 博客,原文链接:http://zhangjunhd.blog.51cto.com/113473/68949 在Java中通过Observable类和Observer接口实现了观察者 ...

  3. C# ado.net oledb方式连接(三)

    oledb 方式连接 class Program { private static string constr = "server=.;database=northwnd;integrate ...

  4. python第三方库安装

    如安装jieba分词库 代码对Python 2/3均兼容 全自动安装:easy_install jieba或者pip install jieba / pip3 install jieba 半自动安装: ...

  5. HDU 5734 Acperience ( 数学公式推导、一元二次方程 )

    题目链接 题意 : 给出 n 维向量 W.要你构造一个 n 维向量 B = ( b1.b2.b3 ..... ) ( bi ∈ { +1, -1 } ) .然后求出对于一个常数 α > 0 使得 ...

  6. [Hihocoder] 字符串排序

    题目 http://hihocoder.com/problemset/problem/1712 题解 https://www.zybuluo.com/wsndy-xx/note/1135606

  7. K - Kia's Calculation(贪心)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. [51nod1666] 最大值

    题面 题解 毒瘤题浪费我大好青春 容易知道, 如果\(l\)和\(r\)位数不一样, 直接选形似\(99..99\)的数, 输出答案即可 \(l\)和\(r\)位数一样的话, 当位数确定的时候, 由于 ...

  9. Zookeeper系列(十四)Zookeeper的数据与存储

    作者:leesf    掌控之中,才会成功:掌控之外,注定失败. 出处:http://www.cnblogs.com/leesf456/p/6179118.html尊重原创,奇文共欣赏: 一.前言 前 ...

  10. markdown简单的使用方法

    转自 https://www.cnblogs.com/math/p/se-tools-001.html 作者:正交分解 1.如何切换编辑器 切换博客园编辑器为MarkDown:MarkDown Edi ...