POJ 1703 Find them, Catch them

【题解-POJ】1703 Find them, Catch them WA,还在找BUG😩。 阅读详情

Find them, Catch them
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 42639 Accepted: 13120

Description

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.) 

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

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

Output

For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."

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.

Source

带权并查集经典题,第一次做带权并查集的题,relation[ i ]存的是 i 和他父节点的关系,0代表同帮,1异帮,

带权并查集需要注意的地方:

1.每次合并的时候,是对两个集合的祖先操作

2.find回溯更新是更新本节点和他父节点的关系

3.注意找好关系,以及表示方式

其余的需要注意的应该就和并查集差不多了。


#include<stdio.h>
int f[100005],relation[100005];
int init(int n)
{
	for(int i=0;i<=n;i++)
	{
		f[i]=i;
		relation[i]=0;
	}
	return 0;
}
int find(int x)
{
	if(x==f[x])
	return x;
	int y=find(f[x]);
	relation[x]=(relation[x]+relation[f[x]])%2;
	return f[x]=y;
}
int merge(char lc,int a,int b)
{
	int t1,t2;
	t1=find(a);
	t2=find(b);
	if(t1==t2)
	{
		if((relation[a]+relation[b])%2==0)
		{
			return 1;
		}
		else
		{
			return 0;
		}	
	}
	if(lc=='D')
	{
		f[t2]=t1;
		relation[t2]=(relation[b]+relation[a]+1)%2;
		
	}
	return -1;
}
int main()
{
	int t,n,m,a,b;
	char ch;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&m);
		init(n);
		while(m--)
		{
			getchar();
			scanf("%c %d %d",&ch,&a,&b);
			if(ch=='D')
			{
				merge(ch,a,b);
			}
			else 
			{
				int flog=merge(ch,a,b);
				if(flog==-1)
				{
					printf("Not sure yet.\n");
				}
				else if(flog==0)
				{
					printf("In different gangs.\n");
				}
				else
				{
					printf("In the same gang.\n");
				}
			}
		}
	}
	return 0;
}




poj1703 Find them, Catch them 这是题目: 3:Find them, Catch them 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, G 阅读详情

相关推荐

制造业批次管理中双计量单位动态转换的实践指南

本文详细介绍了制造业批次管理中双计量单位动态转换的实践指南,特别是在SAP系统中的配置与操作步骤。通过实际案例展示了如何解决采购与生产部门计量单位不一致的问题,提升物料管理效率。重点讲解了批次特性配置、移动类型调整及常见问题排查方法,适用于金属加工、化工等多个行业。

weixin_29228203的博客 168

【并查集】POJ 1703 Find them, Catch them

POJ 1703 : Find them, Catch them 有两大帮派,现在不知道每个人的帮派情况。有两种操作,D x y 表示x和y在不同帮派,A x y查询x和y的帮派情况。 如果在同一帮派,输出In the same gang. 如果不在同一帮派,输出In different gangs. 不确定帮派情况则输出Not sure yet. 这里是POJ 11...

小酷Miki的专栏 229

如何读懂CiteSpace图谱?科研小白必看的节点颜色/年轮/聚类解读手册

本文详细解析CiteSpace图谱的核心视觉元素,包括节点颜色、年轮和聚类标识,帮助科研人员准确解读数据分析结果。通过生物医学案例展示如何从图谱中识别关键文献和研究趋势,提供五步系统解读法和常见错误规避策略,助力科研小白快速掌握文献计量分析技巧。

weixin_29179311的博客 323

POJ - 1703 Find them, Catch them (并查集)

                  POJ - 1703  Find them, Catch them (并查集)          这道题与食物链那一道题是同一类型的,但稍微简单些,题意就不说了                   比普通的并查集多了一个relation [i]数组,记录与父亲节点的关系,在并查集里的元素都是已经它们之间的关系的,在路径压缩与集合合并时不断的更新relation数...

GYH0730的博客 444

C#与工业设备的OPC DA/UA协议通信开发

摘要:本文介绍了C#与工业设备通信的两种OPC协议(OPC DA和OPC UA)的实现方法。OPC DA基于COM/DCOM技术,适用于Windows平台,提供实时数据采集功能;OPC UA则支持跨平台通信,具备更强的安全性和数据建模能力。文章详细展示了如何使用C#通过OPC DA和OPC UA协议读取、写入数据,并提供了代码示例,包括安装相关库、建立连接、订阅数据变化等关键步骤。这些方法为工业自动化中的设备监控与控制提供了实用解决方案。(149字)

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1282

深度学习【46】pix2pixHD

英伟达的pix2pixHD,能够合成高清的2048*1024图片,简直振奋人心。pix2pixHD是我之前介绍过的pix2pix的改进版本,使用多由粗到精的G网络和多尺度D网络(每个D网络都是用了pix2pix中同样的patch技术)。论文一开始用pix2pix尝试合成更高分辨率的图片,但发现效果不好,训练也很不稳定。所以就对pix2pix进行了一顿魔改。我们来看看他们是怎么改进的。由粗到精的G网络

DCD_LIN的博客 1万+

poj 1703 Find them, Catch them(带权并查集)

题目链接:poj 1703 Find them, Catch them 题目大意:在城市中有两个大的帮派,Gang Dragon 和Gang Snake,先在有n个人被抓了,警察队他们进行m次操作,操作分为两种,询问A a b, 要求判断a和b是不是属于同一个团伙,D a b 表示说a b不属于同一个团伙。 解题思路:带权并查集的简单版,权值为1表示和前一个人不属于同一个团

不慌不忙、不急不躁 1112

STM32H750实战:用TIM+DMA驱动ADC/DAC实现音频回环(附CubeMX配置避坑指南)

本文详细介绍了在STM32H750上构建高保真、低延迟音频链路的实战方法。通过配置TIM定时器作为硬件节拍器,精准触发ADC采集与DAC播放,并利用DMA实现数据的自动搬运,全程无需CPU干预。文章重点剖析了CubeMX配置中的关键步骤与常见“坑点”,如ADC的Conversion Data Management模式选择、DMA的FIFO与Burst模式配置,以及STM32H7系列必须开启ADC全局中断等,并提供了完整的代码实现与调试验证指南。

1066

poj1703 Find them Catch them

http://www.elijahqi.win/2018/01/12/poj1703-find-them-catch-them/ ‎ Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city

Elijahqi 385

Minitab田口设计实战:如何用正交试验优化遗传算法参数(附完整操作步骤)

本文详细介绍了如何利用Minitab田口设计进行遗传算法参数优化,通过正交试验方法显著提升效率。文章涵盖实验设计、Minitab操作步骤、结果分析及高级技巧,帮助工程师快速掌握这一工业级优化技术,实现算法性能的显著提升。

wind6的博客 873

私家侦探如何追踪难以找到的人,掌握几个很少人知道的在线免费查询网站,搜索人名、电话号码以及地址的开源网站

私家侦探如何追踪难以找到的人,掌握几个很少人知道的在线免费查询网站,搜索人名、电话号码以及地址的开源网站。

代码讲故事 6517

POJ 1703 Find them, Catch them(种类并查集)

题目地址:POJ 1703 种类并查集水题。 代码如下: #include #include #include #include #include #include #include #include #include #include #include using namespace std; int bin[600000], rank[600000]; int f

scf0920 1282

RISC-V CPU设计(三)---多周期CPU的设计与实现

基本情况 本多周期CPU支持RV32I指令集(fence,ecall,ebreak除外),已通过部分指令的测试,如代码有bug,欢迎指出。 模块划分 代码将CPU划分为以下几个模块: 1.控制单元:ControlUnit 用于产生控制信号,是CPU的大脑,指挥中枢。 2.分支决策单元:BranchUnit 用于决定B型指令是否发生跳转 3.立即数单元:ImmUnit 用于从指令中解析出立即数. 4.寄存器堆:RegisterFile RISC-V的32个寄存器,其中x0寄存器恒为0,不可更改。 5.指令存储

qq_40268672的博客 5550

POJ1703 Find them, Catch them

题目链接:http://poj.org/problem?id=1703 大概思路:题目要确认的有三种结果,所以不能直接找出a的不同集合再与b合并。由于事先并不知道a、b是哪一派别的,所以开一个2*maxn大的数组,分成两个派别:a属于a,a属于a+maxn。每次执行D操作其实就是合并(a,b+maxn)和(a+maxn,b),最后判断a,b是否同一派别,a,b+maxn是否同一派别即可。 TL

sun_apple_123 285

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

POJ 1703——Find them, Catch them【并查集变形】

题目传送门 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs ...

NIRVANA REBIRTH 250

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

【POJ 1703】 Find them, Catch them(关系并查集)

【POJ 1703】 Find them, Catch them(关系并查集) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38951   Accepted: 11987 Description The police office in Tadu City

ACMer' 1310

【POJ 1703 Find them, Catch them】并查集

题目链接:http://poj.org/problem?id=1703 题目大意: 给定m个操作: D x y:x罪犯和y罪犯不在一个团伙。(总共只有两个团伙) A x y:询问x和y罪犯的关系 解题思路: 很容易想到是并查集,但最后还是让我纠结了良久。一直MLE。 对于每次的D x y操作,每次不仅要找到两个不同集合标志头进行更新,还要更新a数组,即...

baidui8622的博客 90

Find them, Catch them POJ - 1703

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which g...

songziyuan_的博客 604

POJ-1703-Find them, Catch them 解题报告

一道标准的关系型

JZQT_T的博客 1336

POJ 1703: Find them, Catch them

非常基础的一道在查询时需要传递其它信息的并查集, 这方面的经典题目首推 POJ 1182 食物链 鉴于博客里没有这种类型的题目就放上来了。 写合并的时候还错误的把两个节点间的距离写成两个根节点之间的距离了, 智商捉急得简直不能多说 = =||| 题目链接:http://poj.org/problem?id=1703 题意: 有一些人分属两个帮派。 每个帮派至少有一个

frog的小池塘 953

POJ 1703 Find them,Catch them

#include<iostream> #include<cstdio> #include<cstring> const int MAXN=200020; int n,m; int set[MAXN]; int set_find(int d) { if(set[d]<0) return d; else retu...

flymoyu的博客 274

POJ1703Find them, Catch them 【种类并查集】

题目链接:http://poj.org/problem?id=1703 题目大意:给n个人,m次询问。A代表询问a, b之间的关系,D代表给出a, b属于不同的帮派。 我的想法: 太菜了,上课的时候没想到这种方法。 思路: 1.感觉有点像2-sat的思想,对于D给出的a, b两人属于不同帮派。我们就将a于b的对立点用并查集维护在一个集合中,也将b于a的对立点连通。 2.查询时...

a6334167的博客 175

【POJ】1703 Find them, Catch them 并查集

Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43420 Accepted: 13356DescriptionThe police office in Tadu City decides to say ends to the chaos, as launch a

chutzpah 1139

POJ1703 Find them, Catch them【并查集】

Find them, Catch themTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 50328 Accepted: 15453DescriptionThe police office in Tadu City decides to say ends to the chaos, as launch actions to roo...

海岛Blog 1078
上一篇: HDU 5500 Reorder the Books
下一篇: Codeforces Round #380 (Div. 2,Based on Technocup 2017 - Elimination Round 2)C - Road to Cine//
jerans
博客等级 码龄10年 54粉丝 255原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值