最大二分匹配 匈牙利算法模板&&POJ 1469 COURSES
2012-07-28 04:08
429 查看
题目:COURSES
来源:ZQUOJ 23124&&POJ 1469
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
Input
Your program should read sets of data from the std input. The first line of the input 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.
Output
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.
Sample Input
Sample Output
来源:ZQUOJ 23124&&POJ 1469
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
Input
Your program should read sets of data from the std input. The first line of the input 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.
Output
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.
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 算法:用匈牙利算法求二分图的最大匹配 解题思路:把每个student和每个course看做一个点,每个student和他visit的course之间连一条边,就构成了一个二分图,再用匈牙利算法求这个图的最大匹配,看是否等于course 的数目,是就YES,否当然是NO啦。 有关匈牙利算法请参考:http://imlazy.ycool.com/post.1603708.html http://blog.csdn.net/china8848/article/details/2287769 [code] AC代码:
#include<stdio.h> #include<string.h> int g[301][301],vis[301],match[301],count; //g表示两点间是否有边,vis标记二分图右边的点是否访问过,match保存匹配的点 int n,m; //n右边的点的个数,m左边的点的个数 int dfs(int v) { int i; for(i=1;i<=n;i++) //遍历右边的顶点 { if(!vis[i]&&g[v][i]) //没访问过且边<v,i>存在 { vis[i]=1; if(dfs(match[i])||!match[i]) { //如果该点还未与其他点匹配,或还能找到其他点能与该点匹配的点match[i]进行匹配,即存在增广路 match[i]=v; //将i与v进行匹配 return 1; } } } return 0; } void max_match() { int i; memset(match,0,sizeof(match)); for(i=1;i<=m;i++) //遍历左边的顶点 { memset(vis,0,sizeof(vis)); if(dfs(i)) //存在增广路径 count++; } } int main() { int t,i,k,j,v; scanf("%d",&t); while(t--) { count=0; memset(g,0,sizeof(g)); scanf("%d%d",&m,&n); for(i=1;i<=m;i++) { scanf("%d",&k); for(j=1;j<=k;j++) { scanf("%d",&v); g[i][v]=1; } } max_match(); //求最大二分匹配 if(count==m) //最大匹配数等于课程数 printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- HDU 2444 The Accomodation of Students(最大二分匹配(匈牙利算法)+二分图判断->模板题目)
- 【二分匹配】【匈牙利算法即由增广路求最大匹配模板】
- hdu 1054 Strategic Game(模板) 最大二分匹配,最小点覆盖 匈牙利算法
- 利用匈牙利算法&Hopcroft-Karp算法解决二分图中的最大二分匹配问题
- 利用匈牙利算法&Hopcroft-Karp算法解决二分图中的最大二分匹配问题 例poj 1469 COURSES
- 图算法之:匈牙利算法(最大二分匹配)
- PKU 2584 - T-Shirt Gumbo (二分图最大匹配 & 匈牙利算法 | 最大流 & EK)
- HDU1083+POJ1469 (匈牙利算法+最大二分匹配)
- 二分图最大匹配(匈牙利算法-DFS增广模板)
- 最大二分匹配(匈牙利算法)
- 二分匹配(匈牙利算法)模板
- 匈牙利算法求二分图的最大匹配/匈牙利算法模板
- 最大权二分匹配—KM算法入门 && 模板
- HDU 1083 Courses(二分匹配匈牙利算法模板题啊)
- Poj1469_匈牙利算法_最大二分匹配
- 【模板】匈牙利算法 二分图最大匹配题模板
- 二分最大匹配--匈牙利算法
- 过山车 hdu2063 匹配、匈牙利算法起步&&最大流
- 匈牙利算法,二分图最大匹配、多重匹配模板
- 二分匹配(匈牙利算法)【模板】