ZOJ1140&&POJ1469&&HDU1083-Courses

来源:互联网 发布:淘宝订单自检通知 编辑:程序博客网 时间:2024/05/22 03:11

Courses

Time Limit: 10 Seconds      Memory Limit: 32768 KB

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:


Input

2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1


Output

YES
NO 


Source: Southeastern Europe 2000


题意:有p门课程和n个学生,每个学生选其中几门课程,能否从中选出p名学生组成一个委员会,使得委员会中每名学生代表一门不同的课程,每门课程在委员会中有一个代表

解题思路:二分图


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>#include <functional>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;int p,n;int x[105],y[305];int s[105],nt[30050],e[30050];int visit[305];bool path(int k){    for(int i=s[k];~i;i=nt[i])    {        int ee=e[i];        if(ee==0) continue;        if(!visit[ee])        {            visit[ee]=1;            if(!y[ee]||path(y[ee]))            {                y[ee]=k;                x[k]=ee;                return 1;            }        }    }    return 0;}void MaxMatch(){    int ans=0;    memset(x,0,sizeof x);    memset(y,0,sizeof y);    for(int i=1;i<=p;i++)    {        if(!x[i])        {            memset(visit,0,sizeof visit);            if(path(i)) ans++;        }    }    if(ans==p) printf("YES\n");    else printf("NO\n");}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d %d",&p,&n);        int cnt=1,ee,k;        memset(s,-1,sizeof s);        memset(nt,-1,sizeof nt);        for(int i=1;i<=p;i++)        {            scanf("%d",&k);            for(int j=1;j<=k;j++)            {                scanf("%d",&ee);                nt[cnt]=s[i],s[i]=cnt,e[cnt++]=ee;            }        }        MaxMatch();    }    return 0;}

0 0