HDU - 1829 A Bug's LIfe 带权并查集

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.
        
 题目大意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b。只需要判断有没有,按题目要求输出。

    思路:带权并查集,使用异或运算,用0 1 表示性别(注意有空行!!!)

#include<stdio.h>
#include<string.h>
#define MAXN 2010
int t, m, n,x,y;
int fa[MAXN],sex[MAXN];
int find(int x)
{
	if (x == fa[x])
		return x;
	else
	{
		int fx = find(fa[x]);
		sex[x] = sex[x] ^ sex[fa[x]];//这里什么意思呢?在x与其他bug有关系时,它的性别就是与其他bug的异或
		return fa[x] = fx;
	}
}
void merge(int x, int y)
{
	int fx = find(x), fy = find(y);
	if (fx != fy)//两只bug没有联系时要建立联系,因为是两组,而且性别最开始都是设为0,所以将两组合并时,性别相同则需变为1,不同则定为0
	{
		fa[fx] = fy;
		sex[fx] = !(sex[x] ^ sex[y]);
	}
}
int main()
{
	scanf("%d", &t);
	int Cas = 0;
	while (t--)
	{
		Cas++;
		int flag = 0;
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++)
		{
			fa[i] = i;
			sex[i] = 0;
		}
		for (int i = 0; i < m; i++)
		{
			scanf("%d%d", &x, &y);
			if (flag)
				continue;
			int fx = find(x), fy = find(y);
			if (fx == fy&&sex[x] == sex[y])
			{
				flag = 1;
			}
			if (fx != fy)//没有联系
				merge(x, y);
		}
		printf("Scenario #%d:\n", Cas);
		if (flag)
		{
			printf("Suspicious bugs found!\n\n");
		}
		else
		{
			printf("No suspicious bugs found!\n\n");
		}
	}
	return 0;
}

这道题与我之前写过的一道题十分相似

CSUOJ 1904: 精灵的交际网

AC代码

#include<stdio.h>
#include<string.h>
#define MAXN 50010
int fa[MAXN],ra[MAXN];
void init()
{
	for (int i = 0; i < MAXN; i++)
	{
		fa[i] = i;
		ra[i] = 0;
	}
}
int find(int a)
{
	if (a == fa[a])
		return a;
	else
	{
		int r = find(fa[a]);
		ra[a] = ra[a] ^ ra[fa[a]];
		return fa[a] = r;
	}
}
void merge(int x, int y)
{
	int fx = find(x), fy = find(y);
	if (fx != fy)
	{
		fa[fx] = fy;
		ra[fx]=(!(ra[x]^ra[y]));
	}
}
int main()
{
	int T,n,k,a,b;
	while (~scanf("%d", &T))
	{
		while (T--)
		{
			init();
			scanf("%d%d", &n, &k);
			int flag = 0,cnt;
			for (int i = 1; i <= k; i++)
			{
				scanf("%d%d", &a, &b);
				if (flag)
					continue;
				int fx = find(a), fy = find(b);
				if (fx == fy&&ra[a] == ra[b])
				{
					flag = 1;
					cnt = i;
				}
				if (fx != fy)
					merge(a, b);
			}
			if (!flag)
				printf("-1\n");
			else
				printf("%d\n", cnt);
		}
	}
	return 0;
}
/**********************************************************************
	Problem: 1904
	User: leo6033
	Language: C++
	Result: AC
	Time:180 ms
	Memory:1508 kb
**********************************************************************/

 

全部评论

相关推荐

2025-12-29 22:46
武汉大学 Java
点赞 评论 收藏
分享
2025-12-18 11:59
广州南方学院 C++
牛客78682892...:直接点还好,总比要了简历也不回的强
点赞 评论 收藏
分享
2025-11-13 20:16
已编辑
厦门理工学院 软件测试
专业嗎喽:硕佬,把学校背景放后面几段,学校背景双非还学院,让人看了就不想往下看。 把实习经历和个人奖项放前面,用数字化简述自己实习的成果和掌握的技能,比如负责项目一次通过率90%,曾4次发现项目潜在问题风险为公司减少损失等等
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务