hdu 1083 Courses

hdu  1083  Courses                  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1083

图论 匈牙利算法

题目大意:有一些课程,一些学生,给出每门课程有哪些学生去上,求是否(每门课都有学生当课代表&&每个学生都当了不同科的课代表)。

题目分析:二分匹配匈牙利

#include<iostream>
#include<cstring>
using namespace std;
int n,p;
bool mark[305],g[305][305];
int match[305];
bool hungary(int x)
{
	for(int i=0;i<n;i++)
	{
		if(!mark[i]&&g[x][i])
		{
			mark[i]=true;
			if(match[i]==-1||hungary(match[i]))
			{
				match[i]=x;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	int sum,t,m;
	cin>>t;
	while(t--)
	{
		cin>>p>>n;
		memset(g,false,sizeof(g));
		for(int i=0;i<p;i++)
		{
			cin>>m;
			while(m--)
			{
				cin>>sum;
				g[i][sum-1]=true;
			}
		}
		sum=0;
		memset(match,-1,sizeof(match));
		for(int i=0;i<p;i++)
		{
			memset(mark,false,sizeof(mark));
			if(hungary(i))sum++;
		}
		if(sum==p)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}
PS:虽然还有很多不明之处,但所幸一遍水过了……




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值