最大二分匹配 匈牙利算法模板&&POJ 1469 COURSES
题目: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
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
AC代码:
1 #include<stdio.h> 2 #include<string.h> 3 int g[301][301],vis[301],match[301],count; //g表示两点间是否有边,vis标记二分图右边的点是否访问过,match保存匹配的点 4 int n,m; //n右边的点的个数,m左边的点的个数 5 int dfs(int v) 6 { 7 int i; 8 for(i=1;i<=n;i++) //遍历右边的顶点 9 { 10 if(!vis[i]&&g[v][i]) //没访问过且边<v,i>存在 11 { 12 vis[i]=1; 13 if(dfs(match[i])||!match[i]) 14 { //如果该点还未与其他点匹配,或还能找到其他点能与该点匹配的点match[i]进行匹配,即存在增广路 15 match[i]=v; //将i与v进行匹配 16 return 1; 17 } 18 } 19 } 20 return 0; 21 } 22 void max_match() 23 { 24 int i; 25 memset(match,0,sizeof(match)); 26 for(i=1;i<=m;i++) //遍历左边的顶点 27 { 28 memset(vis,0,sizeof(vis)); 29 if(dfs(i)) //存在增广路径 30 count++; 31 } 32 } 33 int main() 34 { 35 int t,i,k,j,v; 36 scanf("%d",&t); 37 while(t--) 38 { 39 count=0; 40 memset(g,0,sizeof(g)); 41 scanf("%d%d",&m,&n); 42 for(i=1;i<=m;i++) 43 { 44 scanf("%d",&k); 45 for(j=1;j<=k;j++) 46 { 47 scanf("%d",&v); 48 g[i][v]=1; 49 } 50 } 51 max_match(); //求最大二分匹配 52 if(count==m) //最大匹配数等于课程数 53 printf("YES\n"); 54 else 55 printf("NO\n"); 56 } 57 return 0; 58 }
posted on 2012-07-28 04:08 jumpingfrog0 阅读(202) 评论(0) 编辑 收藏 举报