【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 ...
随机推荐
- 埋点(Event Tracking)vs 无埋点(Codeless Tracking) vs 可视化埋点(Visual Event Tracking)
在理解什么是埋点之前,首先需要了解一些基础知识:(以下摘自:http://www.chinawebanalytics.cn/auto-event-tracking-good-bad-ugly/) 我们 ...
- more/less
more less
- vue-cli3构建多页面应用
创建一个项目hello-world vue create hello-worldcd hello-worldnpm run serve 在src目录下新建pages目录,在pages下新建页面 App ...
- Cogs 734. [网络流24题] 方格取数问题(最大闭合子图)
[网络流24题] 方格取数问题 ★★☆ 输入文件:grid.in 输出文件:grid.out 简单对比 时间限制:1 s 内存限制:128 MB «问题描述: 在一个有m*n 个方格的棋盘中,每个方格 ...
- 《30天自制操作系统》学习笔记--Mac环境搭建
弄了三天了,终于弄好了,先说结果,就是作者在网站上放了os x的工具(hrb.osask.jp,也有linux下的工具,可以自己去下载),也就是说我白忙活了三天... 再说一下这几天都干啥了,主要是想 ...
- JVM——内存结构
一.程序计数器/PC寄存器 (Program Counter Registe) 用于保存当前正在执行的程序的内存地址(下一条jvm指令的执行地址),由于Java是支持多线程执行的,所以程序执行的轨迹不 ...
- CDQ分治的嵌套
CDQ的嵌套 上一篇博客介绍了一下CDQ的入门思想.这里再介绍一下它的进阶,CDQ套CDQ.其实如果对入门思想掌握的透彻,嵌套也是很容易掌握的,思想是一样的. 什么是嵌套 简单地说,有的问题,如果用一 ...
- HTML 行内-块级-行块级
行内元素 相邻元素可以在一行显示直到一行排不下才进行换行. 不可设置宽高.对齐等属性,宽度随内容变化. padding和margin的设置中,水平方向(padding-left...)有效果,垂直方向 ...
- Linux设备驱动程序 之 ioctl
ioctl 除了读取和写入设备之外,大部分驱动程序还需要另外一种能力,即通过设备驱动程序执行各种类型的硬件控制,通常这种需求使用ioctl方法支持,该方法实现了同名的系统调用: 在用户空间,ioctl ...
- 提交本地文件至gitlab已有的项目中(更新gitlab)
gitlab代码更新 gitlab官网 1.安装git git官网 官网下载安装,安装过程一直next即可(路径自己选) 2.clone至本机 格式:git clone url(可转到指定目录克隆) ...