抓住他们!

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

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

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

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

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

  

 #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. Redhat修改语言

    vim /etc/sysconfig/i18n 1 LANG="en_US.UTF-8" 2 SYSFONT="latarcyrheb-sun16" 将LANG ...

  2. BIEE 仪表盘的创建

    1.新建仪表盘 选择共享文件夹里创建的表拖到仪表盘中 保存并运行  也可以新建更多仪表盘页 2.新建仪表盘提示 把提示和表都拖到仪表盘中 3.主从关系:第二张表的结果页面 ——>编辑视图——&g ...

  3. bzoj 3437 斜率优化DP

    写题解之前首先要感谢妹子. 比较容易的斜率DP,设sum[i]=Σb[j],sum_[i]=Σb[j]*j,w[i]为第i个建立,前i个的代价. 那么就可以转移了. /**************** ...

  4. golang thrift 总结一下网络上的一些坑

    我们以hello world来大概分析一下golang中的thrift包,并且扒一扒网络上有关thrift的一些坑 查看源码,服务器定义如下:(详见simple_server.go文件) type T ...

  5. Java线程之间通信

    用多线程的目的:更好的利用CPU的资源.因为所有的多线程代码都可以用单线程来实现. 多线程:指的是这个程序(一个进程)运行时产生了不止一个线程. 并行:多个CPU实例或者多台机器同时执行一段处理逻辑, ...

  6. c中动态使用数组

    #include <iostream> #include <fstream> #include<stdlib.h> #define MAXNUM 200 int I ...

  7. 轻量级应用开发之(01)第一个IOS程序

    一 IPhone轻量级开发 1. 开发环境 Mac 版本: OS X EICap 10.11.3 (15D21) XCode开发版本: Version 7.2.1 (7C1002) 2.简单分析 UI ...

  8. NSKeyedArchiver 类 格式

    结论:NSKeyedArchiver 以健值对+类名的形式保存数据: NSDictionary *tDic = @{@"kxxxx":@"hhhh",@&quo ...

  9. 找出进程中各线程cpu消耗情况

    以root用户执行以下命令,以PID 5423举例: 1,根据top命令,找到占用CPU高的进程,找到PID  PID USER      PR  NI  VIRT  RES  SHR S %CPU ...

  10. while练习:输入一个班级的人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩。

    Console.WriteLine("请输入班级的总人数:"); int count = int.Parse(Console.ReadLine()); ;//声明一个循环变量来记录 ...