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 ...
随机推荐
- 浅议SNMP安全、SNMP协议、网络管理学习
相关学习资料 tcp-ip详解卷1:协议.pdf(重点看25章SNMP部分) http://www.rfc-editor.org/rfc/rfc1213.txt http://www.rfc-edit ...
- Eclipse学习总结(02)-动态项目部署到到本地Tomcat
一.发现问题 在eclipse中新建Dynamic Web Project,配置好本地的tomcat并写好代码后选择Run on Server,但运行后发现在tomcat的安装目录下的webapps并 ...
- jquery------.mouseover()和.mouseout()的高级效果使用
index.jsp <div style="width:100%;height:40px;background-color:#aaa;position:absolute;"& ...
- linux桌面应用开发之折腾
1 起因 需要开发一个wifi定位的应用,最先在android下搞,后来因为多网卡的原因要换平台,经历了windows,最终选择用kaili linux.debian系的linux,开发桌面应用怎么办 ...
- 工具分享——将C#文档注释生成.chm帮助文档
由于最近需要把以前的一个项目写一个文档,但一时又不知道写成怎样的,又恰好发现了可以生成chm的工具,于是乎我就研究了下,感觉还不错,所以也给大家分享下.好了,不多废话,下面就来实现一下吧. 生成前的准 ...
- 不要在初始化方法和dealloc方法中使用Accessor Methods
苹果在<Advanced Memory Management Programming Guide>指出: Don’t Use Accessor Methods in Initializer ...
- java中获取路径中的空格处理(%20)问题
在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getReso ...
- shell中的循环
shell中的循环 for循环 类似于C语言的步长控制 例如: ;i<=;i++)); ); done 将1到10,依次乘以4,然后打印出来. 这里顺便提一下,shell里面表达式的计算,可以有 ...
- 三大框架ssh
一.hibernate a.实体类+映射 b.lib hibernate包+oracle包(oracle安装里找) 映射:从类入手class+属性 a.映射的头文件在:hibernate3.jar- ...
- MyBatis 用户表记录数查询
搭建MyBatis开发环境,实现用户表记录数查询 1.在MyEclipse中创建工程,导入MyBatis的jar包