Courses

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5414    Accepted Submission(s): 2600

Problem Description
Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:

. every student in the committee represents a different course (a student can represent a course if he/she visits that course)

. each course has a representative in the committee

Your program should read sets of data from a text file. The first line of the input file contains the number of the data sets. Each data set is presented in the following format:

P N
Count1 Student1 1 Student1 2 ... Student1 Count1
Count2 Student2 1 Student2 2 ... Student2 Count2
...... 
CountP StudentP 1 StudentP 2 ... StudentP CountP

The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses . from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you'll find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N.

There are no blank lines between consecutive sets of data. Input data are correct.

The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line.

An example of program input and output:

 
Sample Input
2
3 3
3
1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1
 
Sample Output
YES
NO
 
在二分图中两个独立的顶点集合V1,V2。当|V1|<|V2|且最大匹配|M|=|V1|时成为完全匹配。当|V1|=|V2|时称为完美匹配。
比较裸的完全匹配。
  1. #include"cstdio"
  2. #include"cstring"
  3. #include"vector"
  4. using namespace std;
  5. const int MAXN=;
  6. int P,N;
  7. vector<int> G[MAXN*+];
  8. int match[MAXN*+];
  9. int vis[MAXN*+];
  10. void add_edge(int u,int v)
  11. {
  12. G[u].push_back(v);
  13. G[v].push_back(u);
  14. }
  15. bool dfs(int u)
  16. {
  17. vis[u]=;
  18. for(int i=;i<G[u].size();i++)
  19. {
  20. int v=G[u][i],w=match[v];
  21. if(w<||(!vis[w]&&dfs(w)))
  22. {
  23. match[u]=v;
  24. match[v]=u;
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. int bipartite_matching()
  31. {
  32. memset(match,-,sizeof(match));
  33. int ans=;
  34. for(int i=;i<=P;i++)
  35. {
  36. if(match[i]<)
  37. {
  38. memset(vis,,sizeof(vis));
  39. if(dfs(i)) ans++;
  40. }
  41. }
  42. return ans;
  43. }
  44. int main()
  45. {
  46. int T;
  47. scanf("%d",&T);
  48. while(T--)
  49. {
  50. for(int i=;i<MAXN*+;i++) G[i].clear();
  51. scanf("%d%d",&P,&N);
  52. for(int i=;i<=P;i++)
  53. {
  54. int s;
  55. scanf("%d",&s);
  56. while(s--)
  57. {
  58. int v;
  59. scanf("%d",&v);
  60. add_edge(i,v+MAXN);
  61. add_edge(v+MAXN,i);
  62. }
  63. }
  64. int ans=bipartite_matching();
  65. if(ans==P) printf("YES\n");
  66. else printf("NO\n");
  67. }
  68.  
  69. return ;
  70. }
 
 

HDU1083(最大匹配)的更多相关文章

  1. HDU1083 Courses —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-1083 Courses Time Limit: 20000/10000 MS (Java/Others)    Memory ...

  2. HDU-1083 Courses 二分图 最大匹配

    题目链接:https://cn.vjudge.net/problem/HDU-1083 题意 有一些学生,有一些课程 给出哪些学生可以学哪些课程,每个学生可以选多课,但只能做一个课程的代表 问所有课能 ...

  3. hdu2063+hdu1083(最大匹配数)

    传送门:hdu2063过山车 #include <cstdio> #include <cstring> #include <string> #include < ...

  4. HDU1083(KB10-C 二分图最大匹配)

    Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  5. COURSES---poj1469 hdu1083(最大匹配)

    题目链接:http://poj.org/problem?id=1469   http://acm.hdu.edu.cn/showproblem.php?pid=1083 题意:有n个学生p门课, 每门 ...

  6. HDU1083(二分图最大匹配vector实现)

    Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  7. HDU-1083-Courses(最大匹配)

    链接: https://vjudge.net/problem/HDU-1083#author=HUCM201732 题意: 题目大意: 一共有N个学生跟P门课程,一个学生可以任意选一 门或多门课,问是 ...

  8. Python学习实践------正向最大匹配中文分词

    正向最大匹配分词: 1.加载词典文件到集合中,取词典文件中最大长度词的length 2.每次先在句子中按最大长度分割,然后判断分割的词是否存在字典中,存在则记录此词,调整起始点. 3.不存在则按最大长 ...

  9. UOJ79 一般图最大匹配

    题目描述 从前一个和谐的班级,所有人都是搞OI的.有 nn 个是男生,有 00 个是女生.男生编号分别为 1,-,n1,-,n. 现在老师想把他们分成若干个两人小组写动态仙人掌,一个人负责搬砖另一个人 ...

随机推荐

  1. 杂谈之WEB前端project师身价

    了解javascript语言规范么?+1000 知道各浏览器的css差异么?+1000 javascript差异呢?+1000 知道html各标签的含义并不是常好地运用么?+1000 知道怎样跨浏览器 ...

  2. 用Visual C++ 2010 载入动态链接库三部曲(使用第三方库的一般方法)

    以下以载入编译好的ACE动态链接库为例说明:这里如果你已经设置了环境变量ACE_ROOT ACE在VS2010下高速配置载入动态链接库三部曲:(这里如果你的ACE文件夹为E:\ACE_wrappers ...

  3. js随机数 从头开始系列

    js要常常写啊要不然就要从0开始 1 var num = Math.random(); //创建一个0-1随机数字 num*=10 //变为0-10随机数字 //有好几种取整方式 var i = Ma ...

  4. python(32)- 模块练习Ⅱ:使用正则表达式实现计算器的功能

    开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568 ...

  5. 华为OJ 名字美丽度

    这是一道坑爹的题目,为什么这么说,且看我慢慢分析-- 题目例如以下: 给出一个名字,该名字有26个字符串组成,定义这个字符串的"美丽度"是其全部字母"美丽度"的 ...

  6. Django缓存问题

    由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...

  7. mysql查询结果自动生成序列号

  8. C#用Infragistics 导入导出Excel

    最近项目中有数据的导入导出Excel的需求,这里做简单整理. 公司用的是Infragistics的产品,付费,不需要本地安装Office. 有需要的朋友可以下载 Infragistics.2013.2 ...

  9. JQuery 如何获取select选中的值

    一.html代码 <select id="ddl"> <option value="100" emoney="12" &g ...

  10. java之冒泡排序

    //冒泡排序(Bubble Sorting)的基本思想是:通过对待排序序列从后向前(从下标较大的元素开始),依次比较相邻元素的排序码,若发现逆序则交换,使排序码较小的元素逐渐从后部移向前部(从下标较大 ...