hdu2444:The Accomodation of Students(二分图判断+最大匹配)

72 篇文章 0 订阅
30 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=2444

Problem Description

There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.

Calculate the maximum number of pairs that can be arranged into these double rooms.

 

 

Input

For each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.

Proceed to the end of file.
 

 

 

Output

If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.

 

 

Sample Input

4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6

 

 

Sample Output

No
3

题意分析:

有n个学生,有的学生互相认识,有的不认识,现在要求把学生分成两队,每一队之中的所有人互相都不认识,从两队之中各挑出一个人,他们互相认识,求最多可以挑出多少对人。

解题思路:

分成两队就是判断是否是二分图,判断二分图的方法:选取一个点涂成红色,把和它相连的点涂成蓝色,如果最后能够把所有的点涂色并且没有相邻的颜色相同,这个图就是二分图。

然后求一下二分图的最短匹配即可。

#include <stdio.h>
#include <string.h>
#include <vector>
#define N 220
using namespace std;
int e[N][N], book[N], color[N], march[N], n, m;
vector<int>a[N];
int dfs(int u)
{
	int v;
	for(v=1; v<=n; v++)
	{
		if(book[v]==0 && e[u][v]==1)
		{
			book[v]=1;
			if(march[v]==0 || dfs(march[v]))
			{
				march[v]=u;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int ans, i, j, u, v, l, temp;
	while(scanf("%d%d", &n, &m)!=EOF)
	{
		memset(march, 0, sizeof(march));
		memset(color, 0, sizeof(color));
		memset(e, 0, sizeof(e));
		ans=0;
		for(i=1; i<=n; i++)
			a[i].clear();
		for(i=1; i<=m; i++)
		{
			scanf("%d%d", &u, &v);
			a[u].push_back(v);
			a[v].push_back(u);
			e[u][v]=e[v][u]=1;
		}
		temp=1;
		for(i=1; i<=n; i++)
		{
			l=a[i].size();
			if(color[i]==0)
				color[i]=1;
			for(j=0; j<l; j++)
			{
				if(color[a[i][j]]==color[i] && color[i]!=0)
				{
					temp=0;
					break;
				}
				if(color[a[i][j]]==0)
				{
					if(color[i]==1)
						color[a[i][j]]=2;
					else
						color[a[i][j]]=1;
				}
			}
			if(temp==0)
				break;
		}
		if(!temp || n==1)
		{
			printf("No\n");
			continue;
		}
		for(i=1; i<=n; i++)
		{
			memset(book, 0, sizeof(book));
			if(dfs(i))
				ans++;
		}	
		printf("%d\n", ans/2);
	}
	return 0; 
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值