题目链接

http://poj.org/problem?id=1703

题意

有两个帮派:龙帮和蛇帮,两个帮派共有n个人(编号1~n),输入m组数据,每组数据为D [a][b]或A [a][b],D[a][b]表示a,b属于不同的帮派,A [a][b]则让我们判断a,b是否属于一个帮派,根据判断的结果进行相应的输出。

思路

这题和poj2492很像,使用并查集解决,方法我已在poj2492的题解中写出,这里不再赘述。

代码

 #include <cstdio>
using namespace std; const int N = + ;
int p[N];
int r[N]; void make_set(int n)
{
for (int i = ;i <= n;i++)
{
p[i] = -;
r[i] = ;
}
} int find_root(int x)
{
if (p[x] == -)
return x; int t = p[x];
p[x] = find_root(p[x]);
r[x] = (r[x] + r[t]) % ;
return p[x];
} void union_set(int a, int b)
{
int ra = find_root(a);
int rb = find_root(b); if (ra != rb)
{
p[ra] = rb;
r[ra] = (r[a] + r[b] + ) % ;
}
} int main()
{
//freopen("poj1703.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
int n, m;
scanf("%d%d", &n, &m);
make_set(n);
char c;
int a, b;
for (int i = ;i < m;i++)
{
getchar();
scanf("%c%d%d", &c, &a, &b);
if (c == 'A')
{
if (find_root(a) == find_root(b)) //a,b在一个集合里
{
if (r[a] == r[b]) //a,b为同一帮派
puts("In the same gang.");
else puts("In different gangs."); //a,b为不同帮派
}
else puts("Not sure yet."); //a,b不再同一集合里,故不确定
}
else union_set(a, b);
}
}
return ;
}

注意点

1、函数union_set要写成这样:

//正确写法
void union_set(int a, int b)
{
int ra = find_root(a);
int rb = find_root(b); if (ra != rb)
{
p[ra] = rb;
r[ra] = (r[a] + r[b] + ) % ;
}
}

写成如下形式会MLE:

//错误写法,MLE
void union_set(int a, int b)
{
int ra = find_root(a);
int rb = find_root(b); p[ra] = rb;
r[ra] = (r[a] + r[b] + ) % ;
}

2、使用scanf输入。

相似题目

1、poj2492

poj1703 Find them, Catch them(带权并查集)的更多相关文章

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

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

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

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

  3. (中等) POJ 1703 Find them, Catch them,带权并查集。

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

  4. poj 1703 - Find them, Catch them【带权并查集】

    <题目链接> 题目大意: 已知所有元素要么属于第一个集合,要么属于第二个集合,给出两种操作.第一种是D a b,表示a,b两个元素不在一个集合里面.第二种操作是A a b,表示询问a,b两 ...

  5. POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】

      The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...

  6. POJ1703带权并查集(距离或者异或)

    题意:       有两个黑社会帮派,有n个人,他们肯定属于两个帮派中的一个,然后有两种操作 1 D a b 给出a b 两个人不属于同一个帮派 2 A a b 问a b 两个人关系 输出 同一个帮派 ...

  7. poj2492 A Bug's Life(带权并查集)

    题目链接 http://poj.org/problem?id=2492 题意 虫子有两种性别,有n只虫子,编号1~n,输入m组数据,每组数据包含a.b两只虫子,表示a.b为不同性别的虫子,根据输入的m ...

  8. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  9. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  10. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

随机推荐

  1. Shell记录-Shell脚本基础(一)

    Shell 注释: 你可以把注释,在你的脚本如下: #!/bin/bash # Author : Zara Ali # Copyright (c) Tutorialsyiibai.com # Scri ...

  2. jQuery合并单元格以及还原重置

    一.合并rowspan jQuery.fn.rowspan = function(colIdx) { return this.each(function(){ var that; $('tr', th ...

  3. 全链路追踪traceId,ThreadLocal与ExecutorService

    关于全链路追踪traceId遇到线程池的问题,做过架构的估计都遇到过,现在以写个demo,总体思想就是获取父线程traceId,给子线程,子线程用完移除掉. mac上的chrome时不时崩溃,写了一大 ...

  4. Java写的数据库连接池

    原文地址: http://lgscofield.iteye.com/blog/1820521 import java.sql.*; import java.util.Enumeration; impo ...

  5. 生死相依:说说JQuery中die()、live()详解[翻译]

    一个web前端工程师,应该知道jquery的.live()函数,知道它是做什么用的,但是不知它是怎么样工作的,使用起来也是不得得心应手的,甚至也没听说过.die()(去掉bind事件).即使你能熟悉这 ...

  6. thinkphp 修改分页样式表

    原网页:http://blog.csdn.net/u014175572/article/details/53116546 在这里我有先把page的设置做成了一个函数getpage, 将这个方法放到Ap ...

  7. Eclipse中如何改变主题

    童鞋们, eclipse主题太丑?想设置护眼的主题? 看看这些主题: 请移驾: Eclipse Color Themes 怎么设设置? 1. 打开”eclipse marketplace“, 如下图: ...

  8. 在Django中Session的那点事!

    1.session是什么 首先引入度娘的解释:Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 We ...

  9. windbg分析net程序内存泄漏问题

    1       问题简介 有客户反馈,打了最新补丁后,服务器的内存暴涨,一直降不下来,程序非常卡.在客户的服务器上抓了一个dump文件,开始分析. 分析问题的思路: 1.找到是那些资源占用了大量内存? ...

  10. 13-6_mysql索引_1_Mysql_Learning_Notes_20180719_13-6

    mysql索引_1_Mysql_Learning_Notes 二分查找/折半查找法,binary search 一种在有序数组中查找某一特定元素的搜索算法; 二分查找法的优点是比较少次数,查找速度快, ...