poj1703 Find them, Catch them(带权并查集)
题目链接
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(带权并查集)的更多相关文章
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...
- (中等) 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 ...
- poj 1703 - Find them, Catch them【带权并查集】
<题目链接> 题目大意: 已知所有元素要么属于第一个集合,要么属于第二个集合,给出两种操作.第一种是D a b,表示a,b两个元素不在一个集合里面.第二种操作是A a b,表示询问a,b两 ...
- 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 ...
- POJ1703带权并查集(距离或者异或)
题意: 有两个黑社会帮派,有n个人,他们肯定属于两个帮派中的一个,然后有两种操作 1 D a b 给出a b 两个人不属于同一个帮派 2 A a b 问a b 两个人关系 输出 同一个帮派 ...
- poj2492 A Bug's Life(带权并查集)
题目链接 http://poj.org/problem?id=2492 题意 虫子有两种性别,有n只虫子,编号1~n,输入m组数据,每组数据包含a.b两只虫子,表示a.b为不同性别的虫子,根据输入的m ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
随机推荐
- Java并发编程原理与实战三:多线程与多进程的联系以及上下文切换所导致资源浪费问题
一.进程 考虑一个场景:浏览器,网易云音乐以及notepad++ 三个软件只能顺序执行是怎样一种场景呢?另外,假如有两个程序A和B,程序A在执行到一半的过程中,需要读取大量的数据输入(I/O操作),而 ...
- Redis实战(三)CentOS 7上Redis主从复制
一主二从架构 1.一主二从架构图 2.通过命令 mkdir redisCluster创建redis集群文件夹 3.通过命令mkdir 6380 mkdir 6381 mkdir 6382在re ...
- [转载]PayPal为什么从Java迁移到Node.js,性能提高一倍,文件代码减少44%
http://ourjs.com/detail/52a914f0127c763203000008 大家都知道PayPal是另一家迁移到Node.js平台的大型公司,Jeff Harrell的这篇博文 ...
- BZOJ3994 约数个数和
3994: [SDOI2015]约数个数和 Time Limit: 20 Sec Memory Limit: 128 MB Description 设d(x)为x的约数个数,给定N.M,求 I ...
- 博皮设计:HTML/CSS/Javascript 源码共享
首先感谢 sevennight 对我的大力帮助,由此他也成为了我的第一位园友:其次,由于本人并不了解 HTML/CSS,因此几乎都在 李宝亨 设计的 博皮源码 的基础上进行的修改:最后,为了获得 更加 ...
- POJ 1350 Cabric Number Problem (模拟)
题目链接 Description If we input a number formed by 4 digits and these digits are not all of one same va ...
- TCP/IP 网络编程的理解
一.网络各个协议:TCP/IP.SOCKET.HTTP等 网络七层由下往上分别为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 其中物理层.数据链路层和网络层通常被称作媒体层,是网络工程 ...
- JS合并单元格
在Web中经常需要合并单元格,例如对于下面一个表格: <!DOCTYPE html> <html> <head> <meta charset="UT ...
- Servlet笔记5--设置欢迎页面及HTTP状态码404、500
欢迎页面: 代码详解: web.xml配置文件: <?xml version="1.0" encoding="UTF-8"?> <web-ap ...
- jquery easyui 扩展验证
$.extend($.fn.validatebox.defaults.rules, { idcard : {// 验证身份证 validator : function(value) ...