Find them, Catch them(POJ 1703 关系并查集)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 38668 | Accepted: 11905 |
Description
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input
Output
Sample Input
1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
Sample Output
Not sure yet.
In different gangs.
In the same gang. 看食物链那一题看了半天,对并查集的理解还是不够啊!这个取余的方法有点6
#include <stdio.h>
#include <string.h>
const int maxn = ; int n,m;
int p[maxn];//保存父节点
int r[maxn];//保存与父节点的关系,0为同类。1为不同类 void make_set()
{
for(int i = ; i <= n; i++)
{
p[i] = i;
r[i] = ;
}
} int find(int x)
{
if(x == p[x])
return x;
int tmp = p[x]; //记录父亲节点,方便更新r[].
p[x] = find(p[x]);
r[x] = (r[x]+r[tmp])%; //根据子节点与父亲节点的关系和父节点与爷爷节点的关系,推导子节点与爷爷节点的关系
return p[x];
} void merge(int a, int b)
{
int fa = find(a);
int fb = find(b);
p[fa] = fb;
r[fa] = (r[a]+r[b]+)%; //fx与x关系 + x与y的关系 + y与fy的关系 = fx与fy的关系
} int main()
{
int test,a,b;
char str[];
freopen("in.txt","r",stdin);
scanf("%d",&test);
while(test--)
{
scanf("%d %d",&n,&m);
make_set();
while(m--)
{
scanf("%s %d %d",str,&a,&b); if(str[] == 'A')
{
if(find(a) != find(b)) //如果根节点不同,则不能判断关系
{
printf("Not sure yet.\n");
continue;
}
else
{
if(r[a] == r[b])
printf("In the same gang.\n");
else printf("In different gangs.\n");
}
}
else
{
merge(a,b);
}
}
}
return ;
}
Find them, Catch them(POJ 1703 关系并查集)的更多相关文章
- Poj(1703),种类并查集
题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...
- poj 2492(关系并查集) 同性恋
题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...
- poj 1182 (关系并查集) 食物链
题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...
- K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...
- 又见关系并查集 以POJ 1182 食物链为例
简单的关系并查集一般非常easy依据给出的关系搞出一个有向的环,那么两者之间的关系就变成了两者之间的距离. 对于此题: 若u.v不在一个集合内,则显然此条语句会合法(暂且忽略后两条.下同). 那么将f ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- poj 1703 - Find them, Catch them【带权并查集】
<题目链接> 题目大意: 已知所有元素要么属于第一个集合,要么属于第二个集合,给出两种操作.第一种是D a b,表示a,b两个元素不在一个集合里面.第二种操作是A a b,表示询问a,b两 ...
- poj 1182 食物链(关系并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62824 Accepted: 18432 Description ...
- pku 1703(种类并查集)
题目链接:http://poj.org/problem?id=1703 思路;个人觉得本质上还是和带权并查集一样的,只不过多了一个MOD操作,然后就是向量关系图稍微改动一下就变成种类并查集了,对于本题 ...
随机推荐
- 【转】简析SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue
转载地址:http://blog.csdn.net/mn11201117/article/details/8671497 SynchronousQueue SynchronousQueue是无界的,是 ...
- 观《Terminal》之感
读书笔记系列链接地址http://www.cnblogs.com/shoufengwei/p/5714661.html. 经人推荐,用了几天时间欣赏了这部斯皮尔伯格导演的电影<Te ...
- Autolayout-VFL语言添加约束-备
一.VFL语言简介 VFL(Visual format language)语言是苹果为了简化手写Autolayout代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...
- 一键安装IIS的点点滴滴——For所有Microsoft的操作系统(下)
原文 http://www.cnblogs.com/cdts_change/archive/2010/03/09/1681392.html 接着上一篇的讲,下面我们将讨论Windows7.Window ...
- 使用ViewPager实现左右“无限”滑动的万年历
有时候就是这样,研究一个问题,一开始想到了一个觉得可行的方案,然后去尝试:尝试了很久.很多次,已经要放弃了,关掉电脑心里 想这个需求没办法实现:在去上厕所的路上突然想到了一个点子,第二天一试,尼玛,搞 ...
- juicer模板引擎使用
http://www.juicer.name/ <script src="~/Scripts/jquery-1.8.2.min.js"></script> ...
- 【LeetCode练习题】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- thinkjs初试
背景 什么是thinkjs?thinkjs是奇舞团开源的一款NodejsMVC框架,该框架底层基于Promise来实现,很好的解决了Nodejs里异步回调的问题.我为什么会使用thi ...
- jquery ajax 局部table 刷新技术
点击查询:
- Unity 异步加载场景
效果图如下: 今天一直在纠结如何加载场景,中间有加载画面和加载完毕的效果动画! A 场景到 B , 看见网上的做法都是 A –> C –> B. C场景主要用于异步加载B 和 播放一些 ...