HDU - 2444 The Accomodation of Students(二分图判断+最大匹配)

Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

 Status

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


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

const int MAXN = 210;
vector<int>g[MAXN];
int color[MAXN];
bool vis[MAXN];
int link[MAXN];

int n, m;

void init()
{
	for (int i = 0; i <= n; i++)
	{
		g[i].clear();
		color[i] = 0;
	}
}

void add(int a, int b)
{
	g[a].push_back(b);
}

bool Dfs(int v, int c)
{
	color[v] = c;
	for (int i = 0; i < g[v].size(); i++)
	{
		int temp = g[v][i];
		if (color[temp] == c)
			return false;
		if (color[temp] == 0 && !Dfs(temp, -c))
			return false;
	}
	return true;
}

bool dfs(int u)
{
	for (int i = 0; i<g[u].size(); i++)
	{
		int v = g[u][i];
		if (!vis[v])
		{
			vis[v] = 1;
			if (link[v] == -1 || dfs(link[v]))
			{
				link[v] = u;
				return true;
			}
		}
	}
	return false;
}

int match()
{
	int ans = 0;
	memset(link, -1, sizeof(link));
	for (int i = 1; i <= n; i++)
	{
		memset(vis, 0, sizeof(vis));
		if (dfs(i)) ans++;
	}
	return ans;
}

int main()
{
	while (scanf("%d%d", &n, &m) != EOF)
	{
		int u, v;
		
		init();
		
		for (int i = 0; i < m; i++)
		{
			scanf("%d%d", &u, &v);
			add(u, v);
			add(v, u);
		}

		int ret = 1;
		for (int i = 1; i <= n; i++)
		{
			if (color[i] == 0)
			{
				if (!Dfs(i, 1))
				{
					ret = 0; break;
				}
			}
		}

		if (ret)
			cout << (match()>>1) << endl;
		else
			cout << "No" << endl;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

这波lucio来全学了

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

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

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

打赏作者

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

抵扣说明:

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

余额充值