HDU-1083Courses,二分图模板题!

来源:互联网 发布:excel调用单元格数据 编辑:程序博客网 时间:2024/06/05 20:56

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
23 33 1 2 32 1 21 13 32 1 32 1 31 1
 

Sample Output
YESNO
 

Source
Southeastern Europe 2000

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

   题意: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;}



0 0
原创粉丝点击