POJ 1469 COURSES(二分图最大匹配)

来源:互联网 发布:淘宝网站结构图 编辑:程序博客网 时间:2024/06/06 04:55

题意:有P门课和N个学生,每门课可能有0个或多个学生选修想选.现在问你能不能找到一种选课方案,使得P门课每门课都正好只有1个学生选修,且任意两个选了课的学生所选的课都不同?

思路:显然的二分图匹配,该图可以看成是左边有P个点,右边有N个点的二分图.又由于我们想使得课与学生连的边最多且每个学生只能连一门课,每门课也只能有一个学生连接(即每个学生或每门课最多只有一条边依附).所以这就是一个匹配问题且是最大匹配.现在要求这个图的最大匹配,看看能否该最大匹配边数目==P.


#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn=500+10;struct Max_Match{    int n,m;    vector<int> g[maxn];    bool vis[maxn];    int left[maxn];    void init(int n,int m)    {        this->n=n;        this->m=m;        for(int i=1;i<=n;i++) g[i].clear();        memset(left,-1,sizeof(left));    }    bool match(int u)    {        for(int i=0;i<g[u].size();i++)        {            int v=g[u][i];            if(!vis[v])            {                vis[v]=true;                if(left[v]==-1 || match(left[v]))                {                    left[v]=u;                    return true;                }            }        }        return false;    }    int solve()    {        int ans=0;        for(int i=1;i<=n;i++)        {            memset(vis,0,sizeof(vis));            if(match(i)) ans++;        }        return ans;    }}MM;int T;int main(){    int k,n,m;scanf("%d",&T);    while(T--)    {scanf("%d%d",&n,&m);MM.init(n,m);int flag = 1;        for (int i = 1;i<=n;i++)        {            scanf("%d",&k);            if (k){while (k--){int j;    scanf("%d",&j);    MM.g[i].push_back(j);}}elseflag = 0;        }if (flag)           printf("%s\n",MM.solve()==n?"YES":"NO");elseprintf("NO\n");    }    return 0;}


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 Student 1 1 Student 1 2 ... Student 1 Count1
Count2 Student 2 1 Student 2 2 ... Student 2 Count2
... 
CountP Student P 1 Student P 2 ... Student P 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


0 0
原创粉丝点击