首页 > ACM题库 > HDU-杭电 > hdu 2444 The Accomodation of Students-二分图-[解题报告]C++
2014
01-26

hdu 2444 The Accomodation of Students-二分图-[解题报告]C++

The Accomodation of Students

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

No
3

题意:首先判断是否是二分图,如果不是的话输出No,如果是的话输出最大匹配

判断二分图:运用染色法,相邻的两个点间染不同的颜色,如果遇到相邻的点是同一颜色的,则不是二分图

具体实现:通过广搜遍历所有的点,判断是否有相邻的点是同一颜色(一定要遍历所有的点),找最大匹配的时候就用匈牙利算法,最后找到的count要除以2,因为找最大匹配的时候每个点都找了,所以求出的count值是最大匹配的二倍

网上好多代码都过不了这组数据,因为没有遍历所有的点,表示这一题数据有点水

4 3

2 3

2 4

3 4

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=205;
int flag[N];
int map[N][N];
int match[N];
bool link[N];
int n,m;
int bfs()
{
    int j;
	memset(flag,-1,sizeof(flag));
	for(j=1;j<=n;j++)
	{
	if(flag[j]!=-1) continue;
	queue<int> q;
	flag[j]=1;
    q.push(j);
	while(!q.empty())
	{
		int k=q.front();
		q.pop();
		for(int i=1;i<=n;i++)
		{
			if(map[k][i]&&flag[i]==flag[k])
			{
		    	return 0;
			}
			if(map[k][i]&&flag[i]==-1)
			{
				q.push(i);
				flag[i]=1-flag[k];
			}
		}
	}
	}
	return 1;
}
bool find(int x)
{
	int i,k;
	for(i=1;i<=n;i++)
	{
		if(map[x][i]==1)
		{
		  k=i;
		   if(!link[k])
		   {
			link[k]=true;
			if(!match[k]||find(match[k]))
			{
				match[k]=x;
				 return true;
			}
		   }
		}
	}
	return false;
}
int main()
{
	int a,b;
	while(~scanf("%d%d",&n,&m))
	{
	 memset(map,0,sizeof(map));
     while(m--)
	 {
		 scanf("%d%d",&a,&b);
		 map[a][b]=1;
		 map[b][a]=1;
	 }
     if(!bfs())
	 {
		 printf("No\n");continue;
	 }
	 int count=0;
     memset(match,0,sizeof(match));
	 for(int i=1;i<=n;i++)
	 {
		memset(link,false,sizeof(link));
		 if(find(i))
		 {
			 count++;
		 }
	 }
	 printf("%d\n",count/2);
	}
	return 0;
}

 

 

解题转自:http://blog.csdn.net/hrdv676/article/details/8262235


  1. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.