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
**********************************************************************/

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值