二分图最大匹配——POJ 1469

来源:互联网 发布:linux 调试 内核模块 编辑:程序博客网 时间:2024/05/18 21:05

对应POJ题目:点击打开链接


COURSES
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 17976 Accepted: 7086

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抣l 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

23 33 1 2 32 1 21 13 32 1 32 1 31 1

Sample Output

YESNO

Source


题意:有P门课和N个学生,一个学生可以选0~p门课,问是否有一个集合包含P个学生,使得该集合里每个学生分别对应P门课,即每门可都有学生对应

思路:二分图求最大匹配模板,就是用个net[i][j]数组,i表示第i门课程,j表示学生ID。这里用DFS和BFS


DFS

#include<cstdio>#include<cstdlib>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<algorithm>#include<cstring>#include<string>#include<iostream>#define ms(x,y) memset(x,y,sizeof(x))const int MAXN=300+10;const int INF=1<<30;using namespace std;int link[MAXN];int vis[MAXN];int net[MAXN][MAXN];int left,right;int c,n;int dfs(int u){for(int v=1; v<=n; v++){if(!vis[v] && net[u][v]){vis[v] = 1;if(link[v] == -1 || dfs(link[v])){link[v] = u;return 1;}}}return 0;}int MaxMatch(){int num=0;ms(link, -1);for(int i=1; i<=c; i++){ms(vis, 0);if(dfs(i)) num++;}return num;}int main(){//freopen("in.txt","r",stdin);int T;scanf("%d", &T);while(T--){ms(net,0);scanf("%d%d", &c,&n);if(n < c){printf("NO\n");continue;}for(int i=1; i<=c; i++){int t,m;scanf("%d", &t);for(int j=0; j<t; j++){scanf("%d", &m);net[i][m] = 1;}}int ans = MaxMatch();if(ans == c) printf("YES\n");else printf("NO\n");}return 0;}



BFS


#include<cstdio>#include<cstdlib>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<algorithm>#include<cstring>#include<string>#include<iostream>#define ms(x,y) memset(x,y,sizeof(x))const int MAXN=300+10;const int INF=1<<30;using namespace std;int link_l[MAXN];int link_r[MAXN];int vis[MAXN];int pre[MAXN];int net[MAXN][MAXN];int c,n;int MaxMatch(){ms(link_l, -1);ms(link_r, -1);int res = 0;for(int i=1; i<=c; i++){//以左边(即课程)为起点寻找一条增广路ms(vis, 0);//清空标记ms(pre, 0);//清空前驱queue<int>q;q.push(i);int ok=0;while(!q.empty()){int u = q.front();q.pop();for(int v=1; v<=n; v++){if(!vis[v] && net[u][v]){//在右边(即学生)寻找vis[v] = 1;if(link_l[v] == -1){//该点还没有匹配ok = 1;int l = u, r = v;while(l)//利用前驱反转路径{int tmp = link_r[l];link_l[r] = l;link_r[l] = r;l = pre[l];r = tmp;}break;//已经找到增广路,退出}else{//该点已经匹配pre[link_l[v]] = u;//记录前驱q.push(link_l[v]);//继续寻找}}}if(ok) break;//已经找到增广路,退出}if(ok) res++;//有增广路,总值加1}return res;}int main(){//freopen("in.txt","r",stdin);int T;scanf("%d", &T);while(T--){ms(net,0);scanf("%d%d", &c,&n);if(n < c){printf("NO\n");continue;}for(int i=1; i<=c; i++){int t,m;scanf("%d", &t);for(int j=0; j<t; j++){scanf("%d", &m);net[i][m] = 1;}}int ans = MaxMatch();if(ans == c) printf("YES\n");else printf("NO\n");}return 0;}



0 0
原创粉丝点击