HDU 1083

Courses

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4975    Accepted Submission(s): 2390

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

int ma[310][310];
int vis[310],link[310];
int n,m;

bool Find(int x)
{
    for(int i=1;i<=m;i++)
    {
        if(!vis[i]&&ma[x][i])
        {
            vis[i]=1;
            if(!link[i]||Find(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int p,q;
        scanf("%d%d",&n,&m);
        memset(ma,0,sizeof(ma));
        memset(link,0,sizeof(link));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p);
            for(int k=0;k<p;k++)
            {
                scanf("%d",&q);
                ma[i][q]=1;
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(Find(i))
                ans++;
        }
        if(ans==n)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-24 10:30:05

HDU 1083的相关文章

hdu 1083 最大匹配

题意:N个学生 P 个课程  求最大匹配 3 3 //学生 课程 3 1 2 3 //课程1 匹配学生1 2 3 2 1 2 1 1典型的匹配没什么好说的 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include<iostream

HDU 1083 裸的二分匹配

Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3424    Accepted Submission(s): 1650 Problem Description Consider a group of N students and P courses. Each student visits zero, one or

Hdu 1083 Courses(匈牙利算法模版题)

Hdu 1083 题意:给你一个p表示测试组数,给你n和m表示课的个数和学生的个数,接下来n行首数字i表示该堂课的学生代表人数,之后为i个学生编码,问能否为每堂课找到一个学生课代表且不冲突: 题解:匈牙利算法模版 另附简单易懂匈牙利算法讲解:传送门 #include<cstring> #include<cstdio> const int N =305; using namespace std; bool h[N][N]; bool vis[N]; int link[N]; int

HDU 1083 网络流之二分图匹配

http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #include<cstdio> #include<cstring> #define maxm 410 using namespace std; int p,n; int master[maxm]; int linking[maxm][maxm]; int has[maxm]; int sol

hdu 1083 Courses

描述:有p门的课,每门课都有若干学生,现在要为每个课程分配一名课代表,每个学生只能担任一门课的课代表,如果每个课都能找到课代表,则输出"YES",否则"NO". 二分匹配 #include<iostream> #include<cstring> #define maxn 305 using namespace std; int c,s; int rem[maxn][maxn]; int visit[maxn],map[maxn]; int d

hdu 1083 Courses(二分图匹配)

Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4278    Accepted Submission(s): 2036 Problem Description Consider a group of N students and P courses. Each student visits zero, one or

hdu 1083 匈牙利算法模板题

题意:有p个课程,n个学生,每个课程有x个学生喜欢.如果每个课程都至少可以分配一个学生就输出YES,否则输出NO. 题解:匈牙利算法裸题. 代码: #include <cstdio>#include <cstring>#define N 400int Map[N][N],S[N],vis[N],n;bool dfs(int u){    for(int i=1;i<=n;i++){        if(Map[u][i]&&!vis[i]){         

匹配 hdu 1083

***题意:n个学生,p门课,求最大匹配,即p门课是否都有人上*** 匈牙利算法 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cctype> #include<queue> #include<vector> #include<algorithm> us

C - Courses - hdu 1083(模板)

一共有N个学生跟P门课程,一个学生可以任意选一 门或多门课,问是否达成: 1.每个学生选的都是不同的课(即不能有两个学生选同一门课) 2.每门课都有一个代表(即P门课都被成功选过) 输入为: P N(课程数跟学生数) 接着有P行,格式为Count studenti studenti+1 ……studentcount (Count表示对课程1感兴趣的学生数,接着有Count个学生) 如第一行2 1 2表示学生1跟学生2对课程1感兴趣 输出为: 若能满足上面两个要求这输出”YES”,否则为”NO”