hdu1083(二分图,匈牙利模板题)

Courses

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


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 <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
const int N=555;
bool tu[N][N];
int from[N];///记录右边的点如果配对好了它来自哪里
bool use[N];///记录右边的点是否已经完成了配对
int n,m;///m,n分别表示两边的各自数量,n是左边,m是右边
bool dfs(int x)
{
    for(int i=1;i<=m;i++)///m是右边,所以这里上界是m
    if(!use[i]&&tu[x][i])
    {
        use[i]=1;
        if(from[i]==-1||dfs(from[i]))
        {
            from[i]=x;
            return 1;
        }
    }
    return 0;
}
int hungary()
{
    int tot=0;
    memset(from,-1,sizeof(from));
    for(int i=1;i<=n;i++)///n是左边,所以这里上界是n
    {
        memset(use,0,sizeof(use));
        if(dfs(i))
            tot++;
    }
    return tot;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(tu,0,sizeof(tu));
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            int t;
            cin>>t;
            for(int j=0;j<t;j++)
            {
                int tt;
                cin>>tt;
                tu[i][tt]=1;
            }
        }
        if(hungary()==n)
        printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

posted @ 2016-03-10 10:47  martinue  阅读(193)  评论(0编辑  收藏  举报