POJ 1703 Find them, Catch them(并查集拓展)
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
题目大意:有n个人,D a b表示a b位于不同的集合,A a b则代表问a b是否属于同一个集合(在线提问)。
思路:并查集拓展的简单应用,不多讲。就用rela[x]表示x与当前父节点(不一点是根节点)是否相同。
代码(313MS):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = ; int fa[MAXN];
bool rela[MAXN]; int get_set(int x) {
if(x == fa[x]) return x;
else {
int ret = get_set(fa[x]);
rela[x] ^= rela[fa[x]];
return fa[x] = ret;
}
} void unionSet(int x, int y) {
int fx = get_set(x);
int fy = get_set(y);
fa[fy] = fx;
rela[fy] = ^ (rela[x] ^ rela[y]);
} void query(int x, int y) {
int fx = get_set(x);
int fy = get_set(y);
if(fx != fy) puts("Not sure yet.");
else if(rela[x] == rela[y]) puts("In the same gang.");
else puts("In different gangs.");
} int main() {
int T, n, m, x, y;
char c;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
fa[i] = i, rela[i] = ;
while(m--) {
scanf(" %c%d%d", &c, &x, &y);
if(c == 'D') unionSet(x, y);
else query(x, y);
}
}
}
POJ 1703 Find them, Catch them(并查集拓展)的更多相关文章
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
- POJ 1703 Find them, Catch them 并查集,还是有点不理解
题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
随机推荐
- update、commit、trancate,delete
update 用于更新表的数据,使用方式为: update table_name set column_name=值 条件 顺便一提:date数据插入更新应该使用 to_date()格式转换函数例如: ...
- iOS10 开发权限适配设置 崩溃(上传打包后构建版本一直不显示)
ios10 系统必须强制配置系统权限 如果不配置,调试的时候导致崩溃,还会引发包无效的问题,导致上传打包后构建版本一直不显示 解决方案1.在项目中找到info.plist文件,右键点击以 Source ...
- css3响应式布局设计——回顾
响应式设计是在不同设备下分辨率不同显示的样式就不同. media 属性用于为不同的媒体类型规定不同的样式.根绝浏览器的宽度和高度重新渲染页面. 语法: @media mediatype and | n ...
- java实现简单计算器功能
童鞋们,是不是有使用计算器的时候,还要进入运行,输入calc,太麻烦了,有时候甚至还忘记单词怎么拼写,呵呵程序员自己写代码实现,又简单,又方便啊 以下为代码(想要生成可执行工具可参考:http://w ...
- 百度 suggestion 学习demo
其他说明文字就不用写那么多了,代码很简单,相信各位道友都能看懂,看不懂的琢磨一下就可以看懂啦.贴代码!拷贝到自己的电脑中运行文件即可,不需要服务器. <!DOCTYPE html> < ...
- 【C语言】素数判定
题目:素数判定. 编写函数,参数是一个正整数n,如果它是素数,返回1,否则返回0. 分析 质数概念: 质数:除了1之外,只能被它本身整除的正数称为质数 如果这个数能被其他正数整除,说明这个数有两个或以 ...
- boost::asio::ip::tcp中几个重要类型
typedef basic_stream_socket socket; 流式套接字,提供同/异步发送接收数据,连接,绑定,设置套接字选项等功能 对于socket中的connect()方法,它只针对某一 ...
- CentOS 7 以上防火墙简单配置
CentOS 7 版本以上默认的防火墙不是iptables,而是firewalle. 因此CentOS 7 以下的 iptables 的配置不能使用. 查看防火墙状态: systemctl statu ...
- Spring Boot多环境配置
在项目开发阶段,开发环境和实际生产环境是不一样,比如使用的数据库/服务连接配置等.因此,配置多个开发环境profile还是必要的 多环境的配置(yml)方式 配置其实很简单,在resource目录下, ...
- CP-ABE ToolKit 安装笔记
博主论文狗,好久没有来贴博客,最近做实验需要用到属性加密,了解了下CP-ABE,前来记录一下: 网上相关的博文较多,博主看了大部分的,认为下面这两个看完了基本就可以成功安装. 可参见博文: http: ...