HDU2444 The Accomodation of Students(二分图判断+匈牙利匹配)

题目链接

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位学生分成两组,使同一组中的任何两个学生互相认识。若可以达成,则输出最多有多少同学相互认识,否则输出‘No’。

思路

分组方式恰好为二分图,则首先利用染色法判断是否为二分图,若是则进行二分匹配。

代码

#include<map>
#include<stack>
#include<queue>
#include<string>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
vector<int>a[maxn];
bool ch[maxn];
int m,n,u,v,sum,ed[maxn],color[maxn];
bool dfs(int x,int c)
{
    
    
	color[x]=c;
	for(int i=0;i<a[x].size();i++)
	{
    
    
		if(color[a[x][i]]==c)
			return false;
		if(color[a[x][i]]==0&&!dfs(a[x][i],-c))
			return false;
	}
	return true;
}
bool solve()
{
    
    
	for(int i=1;i<=n;i++)
	{
    
    
		if(!color[i])
		{
    
    
			if(!dfs(i,1))
				return false;
		}
	}
	return true;
}
bool find(int x)
{
    
    
	for(int i=0;i<a[x].size();i++)
	{
    
    
		if(!ch[a[x][i]])
		{
    
    
			ch[a[x][i]]=1;
			if(!ed[a[x][i]]||find(ed[a[x][i]]))
			{
    
    
				ed[a[x][i]]=x;
				return true;
			}
		}
	}
	return false;
}
int main()
{
    
    
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	while(scanf("%d%d",&n,&m)!=EOF)
	{
    
    
		for(int i=0;i<=n;i++)
			a[i].clear();
		memset(ed,0,sizeof ed);
		memset(color,0,sizeof color);
		for(int i=1;i<=m;i++)
		{
    
    
			scanf("%d%d",&u,&v);
			a[u].push_back(v);
			a[v].push_back(u);
		}
		if(!solve())
		{
    
    
			printf("No\n");
			continue;
		}
		sum=0;
		for(int i=1;i<=n;i++)
		{
    
    
			if(color[i]==1)
			{
    
    
				memset(ch,0,sizeof ch);
				if(find(i))
					sum++;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

Tip:由于数组较多,所以需要注意各数组类型进行定义,否则会出现无法预料的错误。(QAQ)

猜你喜欢

转载自blog.csdn.net/WTMDNM_/article/details/108628893