Courses

                                                                                                    Time Limit: 20000/10000 MS (Java/Others)   

                                                                                                     Memory Limit: 65536/32768
K (Java/Others)



                                                               -> Link <-

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
 
Source

题目废话了一大堆看了好久,重点就那么几句;

题意:P门课程,n个同学,只要某位同学 visited 过一门课程,那么这位同学就可以是这门课程的代表;求一个集合,满足每门课程都有一个代表,且一位同学只能代表一门课程;

输入:P 课程数目,n 学生数目;接下来P行每行先有一个cout表示此门课程有 cout 位同学 visited 过;

输出:是否能找到这样的一个匹配,是则YES,反之NO\n;

典型的二分图最大匹配,有点像完全匹配,只要判断找到最大匹配是否等于P即可;数据范围都很小,所以用匈牙利算法邻接矩阵实现:

using namespace std;
const int N=300+10;
int p,n,g[N][N],linked[N],v[N];
int dfs(int u)
{
for(int i=1; i<=n; i++)//模板;
if(!v[i]&&g[u][i])
{
v[i]=1;
if(linked[i]==-1||dfs(linked[i]))
{
linked[i]=u;
return 1;
}
}
return 0;
}
void hungary()
{
int num=0,i;
memset(linked,-1,sizeof(linked));
for(i=1; i<=p; i++)
{
memset(v,0,sizeof(v));//注意这里每次都要清楚标记;
if(dfs(i)) num++;
}
if(num==p) printf("YES\n");
else printf("NO\n");
}
int main()
{
int t,i,j,x;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&p,&n);
memset(g,0,sizeof(g));
for(i=1; i<=p; i++)
{
scanf("%d",&x);
while(x--)
{
scanf("%d",&j);
g[i][j]=1;
}
}
if(p>n)//此条件很容易得出;
{
printf("NO\n");
continue;
}
hungary();
}
return 0;
}

最新文章

  1. PagerSlidingTabStrip介绍及使用,让ViewPager更绚丽
  2. Devexpress EXCEL导出
  3. iOS 开发笔记-AFNetWorking https SSL认证
  4. ExtJs学习笔记之TextField
  5. Spark Streaming揭秘 Day20 动态Batch size实现初探(上)
  6. 2013华为校园招聘java实现(大家水个回复啊)
  7. 那些年被我坑过的Python——道阻且长(第五章实用模块讲解)
  8. 畅通工程 HDU - 1863
  9. CSS中让一个div的高度随着另外个一个统计的div的高度变化而变化的代码
  10. Confluence 6 匿名访问远程 API
  11. H5实现轮播
  12. repeter 控制一行中显示几条内容
  13. 容器平台选型的十大模式:Docker、DC/OS、K8S 谁与当先?【转】
  14. C语言--第八周作业评分(5班)
  15. Error:Program type already present: android.arch.lifecycle.LiveData
  16. SpringMVC由浅入深day02_7上传图片
  17. 用navicat远程连接mysql:Can&#39;t connect to MySQL server (10060)
  18. 【转】合格PHP工程师的知识结构
  19. Ubuntu18.04 之jdk安装与环境配置
  20. Chrome cookies folder

热门文章

  1. 51nod 1874 字符串排序
  2. yii2 设置多个入口文件
  3. synchronized(2)修饰方法之:普通方法
  4. 463 Island Perimeter 岛屿的周长
  5. React.js 基本环境安装
  6. Oracle用户角色权限相关视图
  7. WPF学习09:数据绑定之 Binding to List Data
  8. 重写java.lang.String IndexOf()方法,实现对字符串以ASCII规则截取
  9. U9249 【模板】BSGS
  10. vue-element:文件上传七牛之key和异步的问题