抓住他们!

  题目大意:两个黑社会帮派,互相打架,现在你很多条信息,要你确定两个人是否属于不同帮派

  这题很有趣,题目不是直接给你两个人是否是一个帮派的,他给你的是不同帮派的,也就是给你很多个不同的要你找相同的。

  乍看很麻烦,但是还记得我以前发布过一个食物链的题解吗!

  食物链要求维护三个不同关系的集合,我第二种方法用的是偏移集!只用两个集合就搞定问题了。

  这一题比食物链更简单,他只用维护两个就可以了。

  

 #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)的更多相关文章

  1. K - Find them, Catch them POJ - 1703 (带权并查集)

    题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...

  2. (并查集 带关系)Find them, Catch them -- poj -- 1703

    链接: http://poj.org/problem?id=1703 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3676 ...

  3. Find them, Catch them(POJ 1703 关系并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38668   Accepted: ...

  4. 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 ...

  5. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  6. POJ 1703 Find them, Catch them(种类并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41463   Accepted: ...

  7. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  8. 【并查集】模板 + 【HDU 1213、HDU 1232、POJ 2236、POJ 1703】例题详解

    不想看模板,想直接看题目的请戳下面目录: 目录: HDU 1213 How Many Tables[传送门] HDU 1232 畅通工程 [传送门] POJ 2236 Wireless Network ...

  9. 【原创】POJ 1703 && RQNOJ 能量项链解题报告

    唉 不想说什么了 poj 1703,从看完题到写完第一个版本的代码,只有15分钟 然后一直从晚上八点WA到第二天早上 最后终于发现了BUG,题目要求的“Not sure yet.”,我打成了“No s ...

随机推荐

  1. 【HDU 5105】Math Problem

    题意 f(x)=|ax3+bx2+cx+d| 求f(x)在L≤x≤R的最大值. 分析 参数有可能是0,注意分类讨论 1.当a=0时 b=0,f为一次函数(c≠0)或者常数函数(c=0),最大值点在区间 ...

  2. SpringAOP

    首先导包, 我用的是Spring4.0.4;需要这三个包 Spring-AOP-4.0.4.REALEASE.jar + Spring-aspect-4.0.4.REALEASE.jar +aspec ...

  3. POJ2253 Frogger

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34865   Accepted: 11192 Descrip ...

  4. Git Pull 避免用户名和密码方法

    在开发中使用的版本控制器时git , 每次使用命令"git pull"从服务器获得最新代码时,都需要输入用户名和密码,这样浪费了大量的时间和热情,在此背景下,本文在网上找到解决版本 ...

  5. Apple的App Analytics统计平台你必须知道的Q&A整理与翻译

    Apple的App Analytics统计平台你必须知道的Q&A整理与翻译 Apple最近在iTunesConnect里最新发布了App Analytics统计平台,提供了现有友盟统计平台和自 ...

  6. java中抽象类与接口中方法访问修饰符问题 (

    1.抽象类中的抽象方法(其前有abstract修饰)不能用private.static.synchronized.native访问修饰符修饰.原 因如下:抽象方法没有方法体,是用来被继承的,所以不能用 ...

  7. Recruit Coupon Purchase Winner's Interview: 2nd place, Halla Yang

    Recruit Coupon Purchase Winner's Interview: 2nd place, Halla Yang Recruit Ponpare is Japan's leading ...

  8. nuget pack

    nuget spec nuget setApiKey yourkeynuget pack PluginMvc.Framework.csproj -Prop Configuration=Releasen ...

  9. WPF控件模拟双击事件

    Action a = () => { i += ; ) { Interval = }; timer.Elapsed += (sender, e) => { timer.Enabled = ...

  10. [Asp.Net]状态管理(ViewState、Cookie)

    简介 HTTP协议是无状态的.从客户端到服务器的连接可以在每个请求之后关闭.但是一般需要把一些客户端信息从一个页面传送给另一个页面. 无状态的根本原因是:浏览器和服务器使用Socket通信,服务器将请 ...