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操作,然后就是向量关系图稍微改动一下就变成种类并查集了,对于本题 ...
随机推荐
- 1169 二叉树遍历(XCOJ DFS)
给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度≤8). 样例输入 BADC BDCA 样例输出 ABCD #include <iostream> ...
- bower解决js库的依赖管理
从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...
- easy ui 实现gridview效果
前台: // 加载审批步骤列表 function FillStep(flowID) { $('#tbStepList').datagrid({ url: "/System/ApproverS ...
- 【破解】破解ACDSEE15的方法
1.先从官方下载一个ACDSEE15简体中文版 2.下载注册机(点我下载) 3.修改注册表 修改注册表ACDSee 32位:HKEY_LOCAL_MACHINE\SOFTWARE\ACD System ...
- NOI十连测 第五测 T2
思路:考虑建立可持久化线段树,第一层维护的是i这个位置的next位置,第二层,维护的是接下来走这个字符会到哪个节点. 感觉很巧妙啊,不愧是Claris #include<algorithm> ...
- 常用IC封装技术介绍
1.BGA(ball grid array)球形触点陈列,表面贴装型封装之一.在印刷基板的背面按陈列方式制作出球形凸点用 以 代替引脚,在印刷基板的正面装配LSI 芯片,然后用模压树脂或灌封方法进行密 ...
- c++ windows下declspec
一.declspec #ifdef STATIC_LIBS #define DLL_API static #else #define DLL_API __declspec (dllexport) #e ...
- python list 中找连续的数字(由网友处学习)
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #学习这个要求的:http://wsky.org/archives/ ...
- 修改UISearchBar placeholder textColor
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
- python学习之路-6 冒泡算法、递归、反射、os/sys模块详解
算法 冒泡算法 # 冒泡算法就是将需要排序的元素看作是一个个"气泡",最小的"气泡"最先浮出水面,排在最前面.从小到大依次排列. # 代码如下: li = [9 ...