hdu 2444 The Accomodation of Students(判断二分图+匈牙利算法)

The Accomodation of Students


Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4085    Accepted Submission(s): 1873




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
 


Source
2008 Asia Harbin Regional Contest Online


题意:

        n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识。如果可以分成两部分,就算出房间最多需要多少间,否则就输出No

分析:

       先判断是否是二分图,然后用匈牙利算法算出最大匹配。

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <set>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff                                          //INT_MAX
#define inf 0x3f3f3f3f                                          //int??????????????????
#define FOR(i,a) for((i)=0;i<(a);(i)++)                          //[i,a);
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define MEM3(a) memset(a,0x3f,sizeof(a))
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
using namespace std;
template<class T>
T Mint(T a, T b, T c) {
	if (a>b) {
		if (c>b)
			return b;
		return c;
	}
	if (c>a)
		return a;
	return c;
}
template<class T>
T Maxt(T a, T b, T c) {
	if (a>b) {
		if (c>a)
			return c;
		return a;
	}
	else if (c > b)
		return c;
	return b;
}

const int maxn=205;
int T,n,m,k;
int line[maxn][maxn],girl[maxn],rej[maxn];
bool used[maxn];
int a,b,flag;

//染色法判断是否是二分图:有关系的用两种不同颜色染色。通过邻接矩阵搜索,如果有一对有关系,并且颜色是相同的。那么就不是二分图了。
void match(int i,int color){
	for(int j=1;j<=n;j++){
		if(line[i][j]){
			if(!rej[j]){
				rej[j]=-color;
				match(j,-color);
			}
			else if(rej[j]==color){
					flag=1;
					return ;
				}
		}
		if(flag)
			return ;
	}
}

//匈牙利算法
bool dfs(int x){
	for(int i=1;i<=n;i++){
		if(line[x][i]&&!used[i]){
			used[i]=1;
			if(!girl[i]||dfs(girl[i])){
				girl[i]=x;
				return true;
			}
		}
	}
	return false;
}

int hungary(){
	int ans=0;
	for(int i=1;i<=n;i++){
		MEM1(used);
		if(dfs(i))
			ans++;
	}
	return ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
#endif
	while(~sfd(n,m)){
		MEM1(line);
		MEM1(rej);
		MEM1(girl);
		for1(i,0,m){
			sfd(a,b);
			line[a][b]=1;
		}
		flag=0;
		match(1,1);
		if(flag){
			printf("No\n");
		}
		else
			printf("%d\n",hungary());
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值