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 ...
随机推荐
- bootstrap table简洁扁平的表格
使用方法 1.在html页面的head标签中引入Bootstrap库(假如你的项目还没使用)和bootstrap-table.css. <link rel="stylesheet&qu ...
- POJ1941 The Sierpinski Fractal
Description Consider a regular triangular area, divide it into four equal triangles of half height a ...
- php中静态变量和静态方法
1,静态变量:所有对象共享的变量成为静态变量.静态变量类似于全局变量,不过全局变量破坏对象的封装性,因此其对应于面向过程:静态变量对应于面向对象. 2,全局变量,全局变量的使用实例如下,声明全局变量时 ...
- 异步http框架简介&实现原理
1 )说明: Android开源代码:www.github.com 模拟一个异步http请求说明
- 代码重构-4 通用方法 用 static
只要没有用到 this.变量/方法 的,都可以用static 原代码: private string GetPeriodDesc(int lotteryPeriod) { return EnumHe ...
- nginx proxy超时报错 upstream timed out (110: Connec...
环境介绍 服务器:centos6.4服务:nginx proxy 问题描述: 然后查找 /opt/usr/nginx/1.4.0/logs 错误 error.log日志提示如下 2015/01/0 ...
- hud 2502 月之数
I think: AC : import java.util.Scanner; public class Main { public static void main(String[] args) { ...
- Mongodb for C# 分组查询
#region 排序获取集合 static List<BsonDocument> GetPagerWithGroup(string connectionString, string dat ...
- struts2 访问国际化资源 <s:text>作为属性
保留全局级国际化信息资源文件.并在message.properteis中增加一个带有参数的国际化信息. labela = labela in zh_CN labelb = labelb,{0} < ...
- LNMP安装成功的界面
在ubuntu13.10上面安装一个lnmp集成环境. 下面是安装成功的界面. ===========================add nginx and php-fpm on startup ...