首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】2492 - A Bug's Life(带权并查集)

【POJ】2492 - A Bug's Life(带权并查集)

作者头像
FishWang
发布2025-08-26 19:39:24
发布2025-08-26 19:39:24
10900
代码可运行
举报
运行总次数:0
代码可运行

A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u

Submit Status

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

代码语言:javascript
代码运行次数:0
运行
复制
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

Hint

Huge input,scanf is recommended.

其实这种分类的并查集的题可以不用权来做,这样会耗费一点时间,但是既然学到这了,那就还用这个吧。

这道题的输出格式好坑爹,本来还以为要考虑最后一组数据不多加一个换行符的问题,结果PE了。后来才发现输出的时候两个换行符就可以AC了。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <cstring>
int f[1111111],re[1111111];		//re数组表示与其父节点的关系,1为不同性,0为同性 
int find(int x)
{
	if (x!=f[x])
	{
		int t;
		t=f[x];
		f[x]=find(f[x]);
		re[x]=(re[x]+re[t])%2;
	}
	return f[x];
}
int main()
{
	int u;
	int n,m;		//n为总数,m为描述的个数
	int num=1;
	bool flag;
	scanf ("%d",&u);
	while (u--)
	{
		scanf ("%d %d",&n,&m);
		for (int i=1;i<=n;i++)
			f[i]=i;
		memset(re,0,sizeof(re));
		flag=false;
		int x,y;
		while (m--)
		{
			scanf ("%d %d",&x,&y);
			int fx,fy;
			fx=find(x);
			fy=find(y);
			if (fx!=fy)		//如果根结点不同的话需要合并整理
			{
				f[fy]=fx;
				re[fy]=(2-re[y]+re[x]+1)%2;
			}
			else
			{
				if (re[x]==re[y])
					flag=true;
			}
		}
		printf ("Scenario #%d:\n",num++);
		if (flag)
			printf ("Suspicious bugs found!\n\n");
		else
			printf ("No suspicious bugs found!\n\n");
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档