(专题赛)A Bug's Life

本文介绍了一个使用并查集算法解决生物学实验中虫性判断问题的方法。该问题来源于一个假设,即如果三只虫子两两之间的互动表明它们不可能全部为异性,则可能存在同性恋现象。通过构建并查集,可以有效地判断这一假设是否成立。

并查集

#转题意:Professor Hopper专门研究bug的生活习性,他表示若两只bugs的生活习性差别很大,则说明他们可能为不同的性别,但如果出现三只bugs的习性两两差别很大,则有可能出现同性恋的bug了。现在有n只bugs,和生活习性差别很大的m对bugs的编号,问这些bugs中,有没有可能出现同性恋者。题目中给出的数对比如(1 2 ,2 3 ,1 3)是表示交配关系的,而且交配的都默认为理解为异性,这里如果有矛盾的情况,就表明有同性恋存在。比如1和2是异性,2和3为异性,那就表明1和3是同性,但是给出的数对是1和3异性表明有矛盾存在,则有同性恋存在,则输出Suspicious bugs found。

 

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!

 

  1 #include<stdio.h>
  2 
  3 
  4 
  5 int pre[1000],rank[1000];
  6 
  7 
  8 
  9 int find(int x)
 10 
 11 {
 12 
 13 if(x!=pre[x])
 14 
 15 pre[x]=find(pre[x]);
 16 
 17 return pre[x];
 18 
 19 }
 20 
 21 
 22 
 23 void join(int i,int j)
 24 
 25 {
 26 
 27 int x=find(i);
 28 
 29 int y=find(j);
 30 
 31 if(x==y)
 32 
 33 return;
 34 
 35 else
 36 
 37 pre[y]=x;
 38 
 39 }
 40 
 41 
 42 
 43 int main()
 44 
 45 {
 46 
 47 int t,i,m,n,x,y,ti=1;
 48 
 49 scanf("%d",&t);
 50 
 51 while(t--)
 52 
 53 {
 54 
 55 scanf("%d",&n);
 56 
 57 for(i=1;i<=n;i++)
 58 
 59 {
 60 
 61 pre[i]=i;
 62 
 63 rank[i]=0;
 64 
 65 }
 66 
 67 int flag=0;
 68 
 69 scanf("%d",&m);
 70 
 71 while(m--)
 72 
 73 {
 74 
 75 scanf("%d %d",&x,&y);
 76 
 77 if(flag)
 78 
 79 continue;
 80 
 81 if(find(x)==find(y))
 82 
 83 {//若x,y在同一个集合,则证明同性恋存在
 84 
 85 flag=1;
 86 
 87 }
 88 
 89 if(rank[x]==0&&rank[y]==0)
 90 
 91 {
 92 
 93 rank[x]=y; //表明y是x的异性
 94 
 95 rank[y]=x;
 96 
 97 }
 98 
 99 else if(rank[x]==0)
100 
101 {
102 
103 rank[x]=y;
104 
105 join(x,rank[y]);
106 
107 }
108 
109 else if(rank[y]==0)
110 
111 {
112 
113 rank[y]=x;
114 
115 join(y,rank[x]);
116 
117 }
118 
119 else
120 
121 {
122 
123 join(x,rank[y]);
124 
125 join(y,rank[x]);
126 
127 }
128 
129 }
130 
131 printf("Scenario #%d:\n",ti++);
132 
133 if(flag)
134 
135 puts("Suspicious bugs found!");
136 
137 else
138 
139 puts("No suspicious bugs found!");
140 
141 printf("\n");
142 
143 }
144 
145 return 0;
146 
147 }

 

转载于:https://www.cnblogs.com/awsent/p/4266973.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值