A Bug's Life(带权并查集)

36 篇文章 0 订阅
A Bug's Life
Time Limit: 10000MS  Memory Limit: 65536K
Total Submissions: 25074  Accepted: 8159

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.

   题意:

   有T个case,每个case开头包含N(1到2000)只虫子和M(1到1000000)个条件关系。每个条件关系都给出a和b,代表a和b可以进行交配,所以属于不同性别,当给出某条关系a和b是同性别的话,说明两只虫子是同性恋。在给出所有关系后,如果有同性恋,则输出Suspicious bugs found!如果没有的话,则输出No suspicious bugs found!

  

   思路:

   种类并查集,可以用带权的并查集来做。0代表是同性,1代表的是异性。

   

   AC:

#include<stdio.h>
#define max 2000+5
int root[max],rel[max];

int find(int a)
{
	if(root[a]==a) return a;
	int r=find(root[a]);
	rel[a]=(rel[a]+rel[root[a]])%2;
	root[a]=r;
	return r;
}
//找根节点且路径压缩
int main()
{
	int n;
	scanf("%d",&n);
	for(int j=1;j<=n;j++)
	{
		int m,t,temp=0;
		scanf("%d%d",&m,&t);
		for(int i=1;i<=m;i++)
		   root[i]=i,rel[i]=0;
//初始化,一开始自结成树,都为同性
		while(t--)
		{
			int a,b;
			int fa,fb;
			scanf("%d%d",&a,&b);
			fa=find(a);
			fb=find(b);
			if(fa!=fb)
			{
				root[fa]=fb;
				rel[fa]=(rel[b]+1-rel[a]+2)%2;
			}
			else 
			{
				if((rel[a]-rel[b]+2)%2==0) temp=1;
			}
//因为要全部输完才判断是不是同性恋
//所以用temp来标记
		}
		printf("Scenario #%d:\n",j);
		temp?printf("Suspicious bugs found!\n\n"):printf("No suspicious bugs found!\n\n");
	}
	return 0;
}

 

   递归过程分析:

int find(int a)
{
	if(root[a]==a) return a;
	int r=find(root[a]);
	rel[a]=(rel[a]+rel[root[a]])%2;
	root[a]=r;
	return r;
}


     比如找D的根节点,那么就会执行find(D)->find(C)->find(B)->find(A)(图1),找到根节点A后,r变量就会存储A这个节点。这时候就会返回根节点A到find(B)函数的r中,这时候开始路径压缩,根据公式rel[a]=(rel[a]+rel[root[a]]),,那么B路径压缩后的权值就是B本身的权值加上B根节点的权值(B根节点为A)(图2),就是1+0=0,更改权值后,将B的根节点更改为A,返回A节点到find(C)的r中。

     这时候到find(C)函数,C路径压缩后的权值就是C本身的权值加上C根节点的权值(C根节点为B),所以就是1+2=3,更改权值后,将C的根节点从B更改成A,返回A节点到find(B)的r中,以此类推……

     总结全部过程就是D->C->B->A然后A->B->C->D,因为权值一直在累加,所以只要一直加上上一层的权值即可。所以这两句话的顺序不可调转,如果调转的话,就是先改变根节点,再改变权值。更改根节点后,rel[root[a]]则一直等于0,那么最后更改得出来的权值一直只是本身的权值加上0,就相当于没有改变权值,只是单纯把根节点改变而已。

rel[a]=(rel[a]+rel[root[a]])%2;
root[a]=r;

 上面的递归过程是普遍例子,如果对应上本题的话,公式就是rel[a]=(rel[a]+rel[root[a]])%2,如图,然后通过图就可以看出,1和2是异性,1和3是同性,1和4是异性了。


     

     总结:

     注意输出格式,要空出一行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值