hdu 2444 The Accomodation of Students 二分图判断 + 最大匹配

题目链接

题意:给定n个点,m条无向边。首先判断能否得到一个二分图(BFS),如果可以,则进行二分图匹配。


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 205;
const int Mod = 1000000007;
int n,m;
bool vis[maxn];
int cx[maxn],cy[maxn];
int map[maxn][maxn],c[maxn];
bool bipartite( int u )
{
	for( int i = 1; i <= n; i ++ )
	{
		if( map[u][i] )
		{
			if( c[u] == c[i] )	return false;
			if( !c[i] )
			{
				c[i] = 3 - c[u];
				if( !bipartite(i) )  
					return false;  
			}
		}
	}
	return true;
}
int findpath( int u )
{
	for( int i = 1; i <= n; i ++ )
	{
		if( c[i] == 2 && map[u][i] && !vis[i] )
		{
			vis[i] = 1;
			if( cy[i] == -1 || findpath( cy[i] ) )
			{
				cy[i] = u;
				cx[u] = i;
				return true;
			}
		}
	}
	return false;
}
int MaxMatch()
{
	int ans = 0;
	memset( cx,-1,sizeof(cx) );
	memset( cy,-1,sizeof(cy) );
	for( int i = 1; i <= n; i ++ )
	{
		if( c[i] == 1 && cx[i] == -1 )
		{
			memset( vis,0,sizeof(vis) );
			ans += findpath( i );
		}
	}
	return ans;
}
int main()
{
	int a,b;
	while( scanf("%d%d",&n,&m) != EOF )
	{
		memset( c,0,sizeof(c) );
		memset( map,0,sizeof(map) );
		for( int i = 0; i < m; i ++ )
		{
			scanf("%d%d",&a,&b);
			map[a][b] = map[b][a] = 1;
		}
		int ok = 1;
		for( int i = 1; i <= n; i ++ )
		{
			if( c[i] == 0 )	c[i] = 1;
			if( !bipartite( i ) )
				ok = 0;
		}
		if( ok )
		{			
			printf("%d\n",MaxMatch());
		}
		else
			puts("No");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值