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 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 

Source

Southeastern Europe 2000

题意:p门课,n个学生,选课代表,一个学生只能做一个课代表,每门课也有限制谁能当课代表,求问能不能满足所有课都有课代表,满足输出“YES”,否则“NO”。

题解:分别以科目,学生各为一个集合,建边,然后求匹配。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#define N 333

using namespace std;

int n,p;
vector<int>G[N];
int linker[N];
bool used[N];

bool dfs(int u) {
    int v;
    for(v=0; v<G[u].size(); v++) {
        int i=G[u][v];
        if(!used[i]) {
            used[i]=true;
            if(linker[i]==-1||dfs(linker[i])) {
                linker[i]=u;
                return true;
            }
        }
    }
    return false;
}

int hungary() {
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=1; u<=p; u++) {
        memset(used,0,sizeof(used));
        if(dfs(u))  res++;
    }
    return res;
}

int main() {
   // freopen("in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--) {
        for(int i=0; i<N; i++)G[i].clear();
        scanf("%d%d",&p,&n);
        for(int i=1; i<=p; i++) {
            int num,x;
            scanf("%d",&num);
            while(num--) {
                scanf("%d",&x);
                G[i].push_back(x);
            }
        }
        int res=hungary();
        if(res<p)printf("NO\n");
        else     printf("YES\n");
    }
    return 0;
}
时间: 2024-08-07 08:37:38

hdu 1083 Courses(二分图匹配)的相关文章

HDU 1083 Coures(二分图匹配)

解题思路: 裸的匈牙利算法,看最大匹配是否等于P: #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int MAXN = 500; int p, n; int G[MAXN][MAXN]; int ma

MZL&#39;s City (hdu 5352 最小费用流 ||二分图匹配)

MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 719    Accepted Submission(s): 251 Problem Description MZL is an active girl who has her own country. Her big country has N cities num

HDU - 1083 Courses (二分图最大匹配模板)

题意:给出P门课程,N个学生.每一门课程可能有多个学生感兴趣然后我们需要匹配,使得每一门课程都只包含一名对其感兴趣的学生问:能否匹配成立思路:这个就是典型的二分图匹配问题.常用匈牙利算法 完整代码:(一开始写成了无向图....)写成有向图是因为学生是可以剩余的 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; c

hdu 5727 Necklace 二分图匹配

题目链接 给2*n个珠子, n<=9, n个阴n个阳. 然后将它们弄成一个环, 阴阳交替.现在给你m个关系, 每个关系给出a, b. 如果阳a和阴b挨着, 那么a就会变暗. 问你最小变暗几个阳. 我们求出阴的所有全排列, 是9!, 因为形成一个环. 所以可以想象成一个珠子是固定不变的, 剩下n-1个变, 所以就是8!. 然后对于每种情况, 就是我们熟悉的二分图匹配了. 对于两个阴珠之间的空隙, 如果阳珠可以放到这里就连一条边. 然后跑匈牙利匹配就可以了. 9!过不了... #include <

A - Fire Net - hdu 1045(二分图匹配)

题意:一个阵地可以向四周扫射,求出来最多能修多少个阵地,墙不可以被扫射透,阵地不能同行或者或者列(有墙隔着例外) 分析:很久以前就做过这道题..当时是练习深搜来着,不过时间复杂度比较高,现在再看突然发现原来可以用二分图匹配来做,时间soso的 ****************************************************************** #include<stdio.h>#include<string.h>#include<algorit

E - Swap - hdu 2819(简单二分图匹配)

题意:如果可以交换行列,问主对角线能不能全为1 分析:要想主对角线全为1很明显要有N个行列不想同的点就行了,可以用二分图匹配计算出来多能有几个.如果小与N就不能.输出要是对的就行,不必和答案一样 ************************************************************************ #include<stdio.h>#include<algorithm>#include<string.h>using namesp

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 ACM 1083 Courses 二分图最大匹配

题意:p门课,每门课有若干学生,要为每门课分配一名课代表,每个学生只能担任一门课的课代表,若每个课都能找到课代表,则输出"YES",否则"NO". 分析:二分图的最大匹配,对课程.学生关系建立一个图,进行二分图最大匹配,当最大匹配数==课程数时说明能够满足要求,否则不能. #include<iostream> using namespace std; #define N 303 bool cs[N][N]; //cs[i][j]表示学生j是否选i这个课程

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