poj1703 Find them, Catch them(种类并查集
题目地址:http://poj.org/problem?id=1703
题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮。输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定),D x y表示x和y不在一个帮派。
思路:种类并查集。维护一个数组rel[x]表示x和根节点的关系(0表示在一个帮派,1表示不在),初始全为0(自己和自己在一个帮派),更新rel用偏移量(rel[x]表示px->x,D x y表示x->y=1)。由于2要特判,输入里分三类:1.n==2&&opr=='A' 2.opr=='D' 3.其他【由于至少有两个帮派的人,当n==2是两个人一定分别是两个帮派的,输出为different】【初始化时记得n从1到n】
- #include <cstdio>
- using namespace std;
- const int N = +;
- int fa[N], rel[N];
- void init(int n)
- {
- for(int i = ; i <= n; i++)
- {
- fa[i] = i;
- rel[i] = ;
- }
- }
- //注意下查找时更新rel[]里那个px
- int found(int x)
- {
- if(fa[x] == x)
- return x;
- int px = fa[x];
- fa[x] = found(fa[x]);
- rel[x] = (rel[px]+rel[x])%;
- return fa[x];
- }
- void unite(int x, int y)
- {
- int px = found(x);
- int py = found(y);
- if(px != py)
- {
- fa[py] = px;
- rel[py] = (rel[x]++-rel[y])%;
- }
- }
- int main()
- {
- int n, m, t, a, b;
- char opr;
- scanf("%d", &t);
- while(t--)
- {
- scanf("%d%d", &n, &m);
- init(n);
- for(int i = ; i < m; i++)
- {
- getchar();
- scanf("%c%d%d", &opr, &a, &b);
- if(n == && opr =='A')//2要特判。分类一共是A2、其他A、D
- {
- printf("In different gangs.\n");
- }
- else if(opr == 'D')
- unite(a, b);
- else
- {
- int pa = found(a);
- int pb = found(b);
- if(pa != pb)
- printf("Not sure yet.\n");
- else if(rel[a] == rel[b])
- printf("In the same gang.\n");
- else
- printf("In different gangs.\n");
- }
- }
- }
- return ;
- }
【特判2wa了好久!!!!!!!!!!!!!!!!!!!初始化函数也写错了。。刚开始写成<n了!!!!!!!!!!!】
poj1703 Find them, Catch them(种类并查集的更多相关文章
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- poj1703 Find them,Catch them 【并查集】
做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...
- poj1703 Find them, Catch them(并查集)
https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...
- poj1703 Find them, Catch them(并查集的应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32225 Accepte ...
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集
题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...
- poj1703(种类并查集)
题意:有两个犯罪集团,现在有两种操作,D [a] [b]表示a和b是属于不同犯罪集团的,A [a] [b] 是询问你a和b的关系,如果ab属于同一个犯罪集团,输出In the same gang. ...
随机推荐
- DHCP报文交互流程
1.发现阶段,即DHCP客户机寻找DHCP服务器的阶段(DHCPdiscover) DHCP客户机以广播方式(因为DHCP服务器的IP地址对于客户机来说是未知的)发送DHCPdiscover发现信息来 ...
- python调用os模块锁定用户
import timeimport osuser_info = { 'mac': {'pwd': '123', 'count': 0, 'locked': False}, 'tank': {'pwd' ...
- mvn无法下载fastdfs-client-java依赖解决办法
1.下载fastdfs-client-java源码 https://github.com/happyfish100/fastdfs-client-java 2.修改pom.xml文件 添加打包插件 & ...
- 【网摘】将图片地址直接 转为 base64
$(function() { function getBase64Image(img) { //转换为 base64 地址 var canvas = document.createElement(&q ...
- 类似discuz密码的生成规则
/* 生成一个串,uniqid(rand()): uniqid(prefix,more_entropy) 函数基于以微秒计的当前时间,生成一个唯一的 ID. 如果 prefix 参数为空,则返回的字符 ...
- win8.1 virtualbox 安装centos7注意事项
win8.1是64位的,一开始在virtualbox中选择版本时,怎么也选不到64位的,这时要改BIOS设置,把CPU虚拟化改为允许. virtualbox是32位的,没必要非得是64位(64位的也不 ...
- hutoolJava工具类的使用
前言 安装 友情开源项目 Hutool相关博客(软文) 捐赠使用公开 核心(Hutool-core) 克隆 支持泛型的克隆接口和克隆类 类型转换 类型转换工具类-Convert 自定义类型转换-Con ...
- 金币(0)<P2015_1>
金币 (coin.cpp/c/pas) [问题描述] 国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天),每天收到两枚金币:之后三天(第四.五.六天),每天收 ...
- 第4节 Scala中的actor介绍:1、actor概念介绍;2、actor执行顺序和发送消息的方式
10. Scala Actor并发编程 10.1. 课程目标 10.1.1. 目标一:熟悉Scala Actor并发编程 10.1.2. 目标二:为学习Akka做准备 注:Sca ...
- 自定义Toast排队重复显示问题:
原文 http://blog.csdn.net/baiyuliang2013/article/details/38655495Toast是安卓系统中,用户误操作时或某功能执行完毕时,对用户的一种提示, ...