HDU1803 Courses(二分图匹配,匈牙利算法,dfs)

来源:互联网 发布:new event js 位置 编辑:程序博客网 时间:2024/06/15 10:59

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 

思路

二分图匹配模板题,首先是T组数据,然后一个p和n代表有p门课和n个学生
接下来p行每行有一个数m,然后有m个数,代表m个学生对这门课感兴趣

问的是能不能有最大匹配,有的话输出YES,匈牙利算法模板题

代码

#include <bits/stdc++.h>using namespace std;#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3fconst int N=200+20;int p,n,m;vector<int>G[N];int e[N][N];int vis[N],match[N];int dfs(int u){    for(int i=0; i<G[u].size(); i++)    {        int v=G[u][i];        if(!vis[v])        {            vis[v]=1;            if(!match[v]||dfs(match[v]))            {                match[v]=u;                return 1;            }        }    }    return 0;}int main(){    int t,x;    scanf("%d",&t);    while(t--)    {        for(int i=0; i<N; i++)G[i].clear();        mem(match,0);        scanf("%d%d",&p,&n);        for(int i=1; i<=p; i++)        {            scanf("%d",&m);            while(m--)            {                scanf("%d",&x);                G[i].push_back(x);            }        }        int sum=0;        for(int i=1; i<=p; i++)        {            mem(vis,0);            if(dfs(i))sum++;        }        puts(sum==p?"YES":"NO");    }    return 0;}
阅读全文
0 0
原创粉丝点击