POJ_2492 A Bug‘s Life

本文介绍了一道关于判断昆虫实验是否支持性别二元论的编程问题,利用并查集数据结构分析异性关系,发现违背教授假设的可疑交互。

A Bug’s Life

链接

POJ_2492 A Bug’s Life

Description

Background

Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.

Problem

Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

Input

The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

Output

The output for every scenario is a line containing “Scenario #i:”, where i is the number of the scenario starting at 1, followed by one line saying either “No suspicious bugs found!” if the experiment is consistent with his assumption about the bugs’ sexual behavior, or “Suspicious bugs found!” if Professor Hopper’s assumption is definitely wrong.

Sample Input

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

Sample Output

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

Hint

Huge input,scanf is recommended.

解析

本题是一道并查集题目。

但是,由于本题的集合中个元素的关系不同,所以对于一个元素,我们不仅要知道它的祖先是谁,还要知道它与祖先是异性还是同性。

我们可以开一个与祖先数组 p r e pre pre 长度一样的数组 r e a rea rea ( R e l a t i o n s h i p Relationship Relationship ) 来记录每个元素与其祖先的关系。其中,若 r e a i = 0 rea_i=0 reai=0,表示 i i i 与它的祖先 p r e i pre_i prei 是同性;若 r e a i = 1 rea_i=1 reai=1,则表示表示 i i i 与它的祖先 p r e i pre_i prei 是异性。

在做路径压缩时,我们不能直接将祖先 r r r 赋值给 p r e i pre_i prei,而是要先做一次异或运算 r e a i = r e a i ⊕ r e a p r e i rea_i=rea_i \oplus rea_{pre_i} reai=reaireaprei,求出 i i i 与其祖先的关系,再做 p r e i = r pre_i=r prei=r

题目要求我们对给出的异性关系判断真假情况。

若一对异性关系为假,首先这两个元素要有同一个祖先。若两个元素与祖先的关系均为异性或均为同性,那么两个元素的关系必然不可能是异性。只有两个元素一个与祖先互为同性,一个与祖先互为异性,这两个元素才可能互为异性。

两个元素的真实关系我们可以通过 r e a a ⊕ r e a b rea_a \oplus rea_b reaareab 求得,再将该结果与 1 1 1 异或,便可以判断数据给出的关系的真假情况。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
int t,n,m,a,b,fa,fb;
int pre[2001];//pre[i]:i的祖先 
int rea[2001];//rea[i]:i与其祖先的关系(0为同,1为异)
bool flag;
int find_root(int x)
{
	int r=x;
	if(x!=pre[x])
	{
		r=find_root(pre[x]);
		rea[x]^=rea[pre[x]];//先求与祖先的关系
		pre[x]=r;//再进行路径压缩
	}
	return r;
}
int main()
{
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		scanf("%d%d",&n,&m);
		flag=false;
		for(int j=1;j<=n;j++)
		{
			pre[j]=j;
			rea[j]=false;
		}
		for(int j=1;j<=m;j++)
		{
			scanf("%d%d",&a,&b);
			if(flag)
				continue; 
			fa=find_root(a);
			fb=find_root(b);
			if(fa==fb)
			{
				if(rea[a]^rea[b]^1)//判断数据给出的异性关系是否为真
					flag=true;
			}
			else
			{
				pre[fb]=fa;//合并两个集合
				rea[fb]=rea[a]^rea[b]^1;//
			}
		}
		printf("Scenario #%d:\n",i);
		if(flag)
			printf("Suspicious bugs found!\n\n");
		else
			printf("No suspicious bugs found!\n\n");
	}
	return 0;
}
内容概要:本文档是一份关于交换路由配置的学习笔记,系统地介绍了网络设备的远程管理、交换机与路由器的核心配置技术。内容涵盖Telnet、SSH、Console三种远程控制方式的配置方法;详细讲解了VLAN划分原理及Access、Trunk、Hybrid端口的工作机制,以及端口镜像、端口汇聚、端口隔离等交换技术;深入解析了STP、MSTP、RSTP生成树协议的作用与配置步骤;在路由部分,涵盖了IP地址配置、DHCP服务部署(接口池与全局池)、NAT转换(静态与动态)、静态路由、RIP与OSPF动态路由协议的配置,并介绍了策略路由和ACL访问控制列表的应用;最后简要说明了华为防火墙的安全区域划分与基本安全策略配置。; 适合人群:具备一定网络基础知识,从事网络工程、运维或相关技术岗位1-3年的技术人员,以及准备参加HCIA/CCNA等认证考试的学习者。; 使用场景及目标:①掌握企业网络中常见的交换与路由配置技能,提升实际操作能力;②理解VLAN、STP、OSPF、NAT、ACL等核心技术原理并能独立完成中小型网络搭建与调试;③通过命令示例熟悉华为设备CLI配置逻辑,为项目实施和故障排查提供参考。; 阅读建议:此笔记以实用配置为主,建议结合模拟器(如eNSP或Packet Tracer)动手实践每一条命令,对照拓扑理解数据流向,重点关注VLAN间通信、路由选择机制、安全策略控制等关键环节,并注意不同设备型号间的命令差异。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值