HDOJ 1829 A Bug's Life 并查集

本文介绍了一种使用并查集数据结构来解决特定性别分类问题的方法。具体而言,该问题涉及验证一组生物个体之间的交互是否符合两种性别且不存在同性互动的假设。文章详细解释了并查集的实现过程,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

并查集的加强训练
题目如下:
Problem 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.

Source

Recommend
linle
若 A->B 而且 A以前出现过,那么 A.opper与B一定是同性 即相同集合中
另注意,题目要求,输入数据完了以后才输出结果
/* 构造数组opp,初始化为本身编号,若A与B有关,首先进行find(A),find(B)的操作,若根相同,则说明A与B属于同一集合,即同性恋, 否则执行下面的操作,如果A的opp等于本身,说明A的opp未初始化,使之等于B,否则将A的opp与B进行Unition操作,类似的如果B的opp等于本身, 说明B的opp未初始化,使之等于A,否则将B的opp与A进行Unition操作 */ #include <iostream> #define N 2100 using namespace std; struct node { int father; int opper; }hash[N]; void init() // 初始化 { int i; for (i=0;i<N;i++) { hash[i].father=i; hash[i].opper=i; } } int Find(int x) //root 返回根节点 { int root=x,temp; while (root!=hash[root].father) { root=hash[root].father; } while (root!=hash[x].father) // 压缩路径 { temp=hash[x].father; hash[x].father=root; x=temp; } return root; } void Unition(int x,int y) // 合并 { int fx=Find(x); int fy=Find(y); hash[fy].father=fx; } /* 注释内是错误版本的主函数 int main() { int T,i; scanf("%d",&T); for(i=1;i<=T;i++) { int n,m,j,x,y,a,b;; scanf("%d%d",&n,&m); init(); bool flag=0; // 标记有没有发现同性恋 for (j=0;j<m;j++) { scanf("%d%d",&a,&b); x=Find(a); y=Find(b); if (x==y) // 存在同性恋 { flag=1; cout<<"Scenario #"<<i<<":"<<endl; cout<<"Suspicious bugs found!"<<endl; break; // 错误的地方, 虽然输入还没结束便可以确定有没有同性恋存在, // 但是按题目要求必须要等输入完成以后才能进行输出,否则后面输入的数据会紊乱 } if (hash[a].opper!=a) { Unition(y,hash[a].opper); } else { hash[a].opper=y; } if (hash[b].opper!=b) { Unition(x,hash[b].opper); } else { hash[b].opper=x; } } if(flag==0) { cout<<"Scenario #"<<i<<":"<<endl; cout<<"No suspicious bugs found!"<<endl; } cout<<endl; } } */ int main() { int T,i; scanf("%d",&T); for(i=1;i<=T;i++) { int n,m,j,x,y,a,b;; scanf("%d%d",&n,&m); init(); bool flag=0; // 标记有没有发现同性恋 for (j=0;j<m;j++) { scanf("%d%d",&a,&b); x=Find(a); y=Find(b); if(flag==1) { continue; } if (x==y) // 存在同性恋 { flag=1; } if (hash[a].opper!=a) { Unition(y,hash[a].opper); } else { hash[a].opper=y; } if (hash[b].opper!=b) { Unition(x,hash[b].opper); } else { hash[b].opper=x; } } if(flag==1) { cout<<"Scenario #"<<i<<":"<<endl; cout<<"Suspicious bugs found!"<<endl; } else { cout<<"Scenario #"<<i<<":"<<endl; cout<<"No suspicious bugs found!"<<endl; } cout<<endl; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值