kuangbin带你飞专题5 I - A Bug's Life POJ - 2492

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.

 

这种关系的题目一看就是并查集维护。

首先容易搞得是扩展域并查集。

异性,再异性就是同行。

一个同性域,和一个异性域。

题目给的都是性别不同,那合并的时候,x的同性域与y的异性域合并。

判断的时候,如果x的同性域与y的同性域相同,那就有矛盾。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const int M = 4000 +100;
int fa[M];
int get(int x)
{
	if(x==fa[x])return x;
	return fa[x]=get(fa[x]);
}
int main()
{
  	int t,n,m,x,y;
  	cin>>t;
  	for(int k = 1; k <= t; k++)
  	{
  		scanf("%d%d",&n,&m);
  		bool f=true;
  		for(int i = 0; i <= n+n; i++)fa[i]=i;
  		for(int i = 1; i <= m; i++)
  		{
  			scanf("%d%d",&x,&y);
  			int gx_same=get(x),gy_same=get(y);
  			int gx_differ=get(x+n),gy_differ=get(y+n);
  			if(gx_same==gy_same||gx_same==gy_differ)
  			{
  				if(gx_same==gy_same)
  					f=false;
			}
			else
			{
				fa[gx_same]=gy_differ;
				fa[gx_differ]=gy_same;
			}
		}
		if(!f)printf("Scenario #%d:\nSuspicious bugs found!\n\n",k);
		else printf("Scenario #%d:\nNo suspicious bugs found!\n\n",k);
	}
	return 0;
}

第二种是带权并查集

边权代表与父亲的关系。

0是同性,1是异性。

如果x,y在一个集合就可以根据他们分别和祖宗的关系找出x,y的关系。

如果不在一个集合,则合并,找出x,y集合祖宗的关系,x祖宗的边权赋值成向量差。

然后x集合里的其他边权可以用路径压缩进行改变。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const int M = 4000 +100;
int fa[M],d[M];
int get(int x)
{
	if(x==fa[x])return x;
	int root=get(fa[x]);
	d[x]=(d[x]+d[fa[x]])%2;
	return fa[x]=root;
}
int main()
{
  	int t,n,m,x,y;
  	cin>>t;
  	for(int k = 1; k <= t; k++)
  	{
  		scanf("%d%d",&n,&m);
  		bool f=true;
  		for(int i = 0; i <= n+n; i++)fa[i]=i,d[i]=0;
  		for(int i = 1; i <= m; i++)
  		{
  			scanf("%d%d",&x,&y);
  			int gx=get(x),gy=get(y);
  			if(gx==gy)
  			{
  				if((d[x]+d[y])%2==0)
  					f=false;
			}
			else
			{
				fa[gx]=gy;
				d[gx]=(3-(d[x]+d[y]))%2;
			}
		}
		if(!f)printf("Scenario #%d:\nSuspicious bugs found!\n\n",k);
		else printf("Scenario #%d:\nNo suspicious bugs found!\n\n",k);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值