POJ 1703 Find them, Catch them 并查集,还是有点不理解
题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派。于是需要一个数组r[]来判断父亲节点和子节点的关系。具体思路可参考http://blog.csdn.net/freezhanacmore/article/details/8774033
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- using namespace std;
- const int maxn=100005;
- int f[maxn];
- int r[maxn];
- int Find_f(int x)
- {
- if (x != f[x])
- {
- int t=f[x];
- f[x] = Find_f(f[x]);
- r[x]=(r[x]+r[t])%2;
- return f[x];
- }
- return f[x];
- }
- void Union(int x,int y)
- {
- int fx=Find_f(x);
- int fy=Find_f(y);
- f[fx]=fy;
- r[fx]=(r[x]+1+r[y])%2;
- }
- int main()
- {
- //freopen("in.txt","r",stdin);
- int t;
- scanf("%d",&t);
- while(t--)
- {
- int n,m;
- scanf("%d%d",&n,&m);
- for(int i=1; i<=n; i++)
- {
- f[i]=i;
- r[i]=0;
- }
- char c;
- int crime1,crime2;
- while(m--)
- {
- getchar();
- scanf("%c%d%d",&c,&crime1,&crime2);
- //printf("%c\n",c);
- if(c=='D')
- Union(crime1,crime2);
- else if(c=='A')
- {
- if(Find_f(crime1)==Find_f(crime2))
- if(r[crime1]!=r[crime2])
- printf("In different gangs.\n");
- else
- printf("In the same gang.\n");
- else
- printf("Not sure yet.\n");
- }
- }
- }
- return 0;
- }
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
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条臭 ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
随机推荐
- Mysql数据库-多实例主从复制-主从故障详解
一.mysql结构 1.实例 1.什么是单实例 一个进程 + 多个线程 + 一个预分配的内存空间 2.多实例 多个进程 + 多个线程 + 多个预分配的内存空间 ](http://shelldon.51 ...
- IDEA 2019.2.4 破解安装教程
将下载的 IDEA 压缩包解压,找到 idealIU-2019.2.4.exe 安装文件,然后双击进行安装 安装完后不要运行,打开解压包中破解补丁与激活码文件夹,找到 jetbrains-agent. ...
- CGI开发-(转自 jemofh159)
随着Internet技术的兴起,在嵌入式设备的管理与交互中,基于Web方式的应用成为目前的主流,这种程序结构也就是大家非常熟悉的B/S结构,即在嵌入式设备上运行一个支持脚本或CGI功能的Web服务器, ...
- 源码安装nginx env
源码安装nginx 1. For ubuntu:18.04 apt -y install build-essential libtool libpcre3 libpcre3-dev zlib1g-de ...
- KMP算法中我对获取next数组的理解
之前在学KMP算法时一直理解不了获取next数组的函数是如何实现的,现在大概知道怎么一回事了,记录一下我对获取next数组的理解. KMP算法实现的原理就不再赘述了,先上KMP代码: 1 void g ...
- 死磕nginx系列
死磕nginx系列 死磕nginx系列--nginx入门 死磕nginx系列--nginx配置文件 死磕nginx系统--nginx日志配置 死磕nginx系列--nginx服务器做web服务器 死磕 ...
- Python+Selenium学习笔记2 - 字符串
跟着网络课程学了几个小程序. 1.判断a字符串是否为b字符串的子串 1 # coding = utf-8 2 3 # 判断str_a字符串是否为str_b字符串的子串 4 5 str_a = &quo ...
- Halide应用开发
Halide应用开发 1. 基本原理 1.1.介绍 随着人工智能的普及,深度学习网络的不断涌现,为了让各硬件(CPU, GPU, NPU,...)能够支持深度学习应用,各硬件芯片需要软件库去支持高性能 ...
- Amazon SageMaker和NVIDIA NGC加速AI和ML工作流
Amazon SageMaker和NVIDIA NGC加速AI和ML工作流 从自动驾驶汽车到药物发现,人工智能正成为主流,并迅速渗透到每个行业.但是,开发和部署AI应用程序是一项具有挑战性的工作.该过 ...
- 语义分割:基于openCV和深度学习(一)
语义分割:基于openCV和深度学习(一) Semantic segmentation with OpenCV and deep learning 介绍如何使用OpenCV.深度学习和ENet架构执行 ...