hdu 1083 Courses

来源:互联网 发布:张思然淘宝店名字 编辑:程序博客网 时间:2024/05/20 16:12

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:虽然还有很多不明之处,但所幸一遍水过了……




原创粉丝点击