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. spark-聚合算子aggregatebykey

    spark-聚合算子aggregatebykey Aggregate the values of each key, using given combine functions and a neutr ...

  2. Java8-Stream-No.01

    import java.util.ArrayList; import java.util.List; import java.util.Optional; public class Streams1 ...

  3. PHP mysqli_options() 函数

    定义和用法 mysqli_options() 函数设置额外的连接选项,用于影响连接行为. mysqli_options() 函数可以被调用若干次来设置若干个选项. <?php $con=mysq ...

  4. [Cogs] 线型网络

    题面 http://cogs.pro:8080/cogs/problem/problem.php?pid=6 题解 https://www.zybuluo.com/wsndy-xx/note/1135 ...

  5. Codevs 1629 01迷宫

    1629 01迷宫 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 有一个由01组成的n*n格迷宫,若你位于一格0上,那么你可 ...

  6. Java学习笔记(持续更新ing)

    1.在读入字符串时:    str = sc.nextLine();     //读入一行                                     str = sc.next();   ...

  7. mac使用brew安装mysql报RROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    使用mac安装mysql安装完后运行 mysql -uroot -p 报了 ERROR 2002 (HY000): Can't connect to local MySQL server throug ...

  8. python pip 使用

    一.修改 pip 源 Windows 在用户目录下新建 pip\pip.ini 文件,如:C:\Users\Administrator\pip\pip.ini [global] index-url = ...

  9. laravel-5.3(1) 路由配置

    第一步: 按照上一篇搭建好工程后可以看到框架默认的 welcome 默认视图: 一般的web 开发框架是MVC设计模式,那么我们现在创建自己的控制器和视图,CMD 进入到工程根目录执行 php art ...

  10. 在Ubuntu 16.04 LTS下编译安装OpenCV 4.1.1

    目录 一 安装前的准备 二 编译并安装OpenCV 4.1.1 注:原创不易,转载请务必注明原作者和出处,感谢支持! OpenCV目前(2019-8-1)的最新版本为4.1.1.本文将介绍如何在Ubu ...