hdu 1083 Courses (二分图匹配)

Courses

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


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
题意:有p门的课,每门课都有若干学生,现在要为每个课程分配一名课代表,每个学生只能担任一门课的课代表,如果每个课都能找到课代表,则输出"YES",否则"NO"。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 300
using namespace std;

int p,n,m,ans;
int nx,ny;         //X 和 Y 集合中顶点的个数
int g[maxn][maxn];               // 存图
int cx[maxn],cy[maxn],mk[maxn];  // cx[i]表示最大匹配中与 Xi 匹配的 Y 顶点, cy[i]同理
                                 // mk[]记录每次增广中cy[]是否被访问
int path(int u)
{
    int v;
    for(v=1;v<=ny;v++)    // 考虑所有 Yi 顶点 v
    {
        if(g[u][v]&&!mk[v])    //v 与u邻接,且没有访问过
        {
            mk[v]=1;
            if(!cy[v]||path(cy[v]))   // 如果 v 没有匹配,或者 v 已经匹配了,但从 cy[v] 出发可以找到一条增广路
            {                         // 注意如果前一个条件成立,则不会递归调用
                cx[u]=v;              // 记录匹配
                cy[v]=u;
 //               printf("u:%d v:%d\n",u,v);
                return 1;
            }
        }
    }
    return 0;
}
void march()
{
    int i,j;
    ans=0;
    memset(cx,0,sizeof(cx));
    memset(cy,0,sizeof(cy));
    for(i=1;i<=nx;i++)
    {
        if(!cx[i])                  // 从每个未盖点出发进行寻找增广
        {
            memset(mk,0,sizeof(mk));
            ans+=path(i);           // 每找到一条增广路,可使得匹配数加 1
        }
    }
}
int main()
{
	int i,j,t,temp;
	scanf("%d",&t);
	while(t--)
	{
	    memset(g,0,sizeof(g));
		scanf("%d%d",&p,&n);
		nx=p;
		ny=n;
		for(i=1;i<=p;i++)
        {
            scanf("%d",&m);
            for(j=1;j<=m;j++)
            {
                scanf("%d",&temp);
                g[i][temp]=1;
            }
        }
        march();
        if(ans==nx)  printf("YES\n");   // 如果课程完全匹配 则满足条件
        else  printf("NO\n");
	}
	return 0;
}


你可能感兴趣的:(hdu 1083 Courses (二分图匹配))