hdu3478 Catch (二分图判断)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478


题解:使某一时刻到达该图任意顶点:1.图是联通的,2.无向图的每一个顶点必须保证能够在奇时刻或偶时刻可以进行访问.(二部图的顶点两个顶点集合只能在奇或偶时刻对齐进行访问)


#include <cstdio>
#include <cstring>
#include <vector>
#define MAXN 100002
using namespace std;

vector<int> mp[MAXN];
int color[MAXN],n;
int Que[MAXN];

int Scan()  
{  
	char ch;  
	int ret=0;  
	while((ch=getchar())<'0'||ch>'9');  
	while(ch>='0'&&ch<='9')  
	{  
		ret=ret*10+(ch-'0');  
		ch=getchar();  
	}  
	return ret;  
}

int BFS(int s)
{
	int start=0,end=1;
	int num,i,temp,x,flag,size;
	flag=0;
	num=1;
	memset(color,-1,sizeof(color));
	Que[0]=s;
	color[s]=0;
	while(start<end)
	{
		x=Que[start];
		size=mp[x].size();
		for(i=0;i<size;++i)
		{
			temp=mp[x][i];
			if(color[x]==color[temp])
			{
				flag=1;//不是二分图
				continue;
			}
			if(color[temp]==-1)
			{
				num++;
				color[temp]=color[x]^1;
				Que[end++]=temp;
			}
		}
		start++;
	}
	if(flag&&(num==n))
		return 1;
	else
		return 0;
}

int main()
{
	int i,m,s,test,u,v,ans,cases=1;
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d %d %d",&n,&m,&s);
		for(i=0;i<=n;++i)
			mp[i].clear();
		while(m--)
		{
			u=Scan();
			v=Scan();
			mp[u].push_back(v);
			mp[v].push_back(u);
		}
		printf("Case %d: ",cases++);
		if(BFS(s))
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值