DisJSet:Find them, Catch them(POJ 1703)

题目大意:两个黑社会帮派,互相打架,现在你很多条信息,要你确定两个人是否属于不同帮派
这题很有趣,题目不是直接给你两个人是否是一个帮派的,他给你的是不同帮派的,也就是给你很多个不同的要你找相同的。
乍看很麻烦,但是还记得我以前发布过一个食物链的题解吗!
食物链要求维护三个不同关系的集合,我第二种方法用的是偏移集!只用两个集合就搞定问题了。
这一题比食物链更简单,他只用维护两个就可以了。
#include <iostream>
#include <functional>
#include <algorithm>
#define MAX 100005 using namespace std;
typedef int Position; static int Set[MAX];
static int delta[MAX]; Position Find(Position);
void Unite_Set(Position, Position);
void Test(Position, Position); int main(void)
{
int case_sum, n, message_sum, tmp_x, tmp_y;
char choice;
scanf("%d", &case_sum);
while (case_sum--)
{
scanf("%d%d", &n, &message_sum);
getchar();
for (int i = ; i <= n; i++)
Set[i] = i;
memset(delta, , sizeof(delta));
for (int i = ; i < message_sum; i++)
{
scanf("%c", &choice);
scanf("%d%d", &tmp_x, &tmp_y);
if (choice == 'D')
Unite_Set(tmp_x, tmp_y);
else
Test(tmp_x, tmp_y);
getchar();//消除回车
}
}
return ;
} void Test(Position x, Position y)
{
Position px, py;
px = Find(x); py = Find(y);
if (px == py)
{
if ((delta[x] - delta[y] + ) % == )
puts("In the same gang.");
else
puts("In different gangs.");
return ;
}
puts("Not sure yet.");
} Position Find(Position x)
{
Position tmp;
if (Set[x] == x)
return x;
tmp = Find(Set[x]); delta[x] = (delta[x] + delta[Set[x]]) % ;
Set[x] = tmp;
return tmp;
} void Unite_Set(Position x, Position y)
{
Position px, py;
px = Find(x); py = Find(y);
if (px != py)
{
Set[py] = px;
delta[py] = (delta[x] - delta[y] + + ) % ;
}
}

DisJSet:Find them, Catch them(POJ 1703)的更多相关文章
- K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...
- (并查集 带关系)Find them, Catch them -- poj -- 1703
链接: http://poj.org/problem?id=1703 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3676 ...
- Find them, Catch them(POJ 1703 关系并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38668 Accepted: ...
- Find them, Catch them POJ - 1703
题意:N个人,M次操作,操作一:A X Y,X,Y不是同一帮派,操作二:D X Y,判断X和Y的关系. 思路:如果X和Y不是同一帮派,那X与Y+N.Y与X+N是同一帮派,如果X与Y不在同一帮派且X与Y ...
- poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- 【并查集】模板 + 【HDU 1213、HDU 1232、POJ 2236、POJ 1703】例题详解
不想看模板,想直接看题目的请戳下面目录: 目录: HDU 1213 How Many Tables[传送门] HDU 1232 畅通工程 [传送门] POJ 2236 Wireless Network ...
- 【原创】POJ 1703 && RQNOJ 能量项链解题报告
唉 不想说什么了 poj 1703,从看完题到写完第一个版本的代码,只有15分钟 然后一直从晚上八点WA到第二天早上 最后终于发现了BUG,题目要求的“Not sure yet.”,我打成了“No s ...
随机推荐
- jQuery 文本编辑器插件 HtmlBox 使用
0.htmlbox下载地址:http://download.csdn.net/detail/leixiaohua1020/6376479 1.引入头文件 <script src="li ...
- 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点
1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1685 Solved: 724[Submit] ...
- 【bzoj3246】 Ioi2013—Dreaming
www.lydsy.com/JudgeOnline/problem.php?id=3246 (题目链接) 题意 给出一棵不完全的树,要求在树上连最少的边使得所有点联通,并且使得两点间最大距离最小. S ...
- POJ2253 Frogger
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34865 Accepted: 11192 Descrip ...
- vim YouCompleteMe
http://www.ithao123.cn/content-1906969.html http://www.it165.net/os/html/201503/12190.html
- 深入浅出Redis03 使用Redis数据库(hashes类型)
一 hashes类型及操作 Redis hash是一个string类型的field和value的映射表.它的添加,删除操作都是0(1)(平均).hash特别适合用于存储对象,相较于对将对象的每个字段存 ...
- C#实现自动升级(附源码)
http://blog.csdn.net/zhuweisky/article/details/50439386 OAUS
- MyEclipse------带进度条的输入流
other.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...
- CentOS 6.4安装Apache+MySQL+PHP的图文教程
LAMP 实际上就是 Linux.Apache.MySQL.PHP 四个名称的缩写,当然最后一个 “P” 还有其他说法是 Perl 或者 Python.不用多说了,本文讲解的就是 Linux.Apac ...
- 基于REST架构的Web Service设计
来自: http://www.williamlong.info/archives/1728.html 先前我曾经介绍过利用Apache Axis实现基于SOAP的Web Service实现技术和相关代 ...