POJ1469_COURSES(二部图最大匹配)

解决报告

http://blog.csdn.net/juncoder/article/details/38136065

题目传送门

题意:

n个学生p门课程,每一个学生学习0或1以上的课程。

问:能否够组成委员会。满足

每一个学生代表一门不同的课程

一门课程在委员会中有一名代表

思路:

非常明显的二分图的完备匹配。

#include <map>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 330
#define P 110
using namespace std;
int mmap[N+P][N+P],n,p,pre[N+P],vis[N+P],m,k;
int dfs(int x)
{
    for(int i=p+1;i<=p+n;i++)
    {
        if(!vis[i]&&mmap[x][i])
        {
            vis[i]=1;
            if(pre[i]==-1||dfs(pre[i]))
            {
                pre[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            memset(mmap,0,sizeof(mmap));
            memset(pre,-1,sizeof(pre));
            scanf("%d%d",&p,&n);
            for(i=1;i<=p;i++)
            {
                scanf("%d",&m);
                for(j=1;j<=m;j++)
                {
                    scanf("%d",&k);
                    mmap[i][k+p]=1;
                }
            }
            int ans=0;
            for(i=1;i<=p;i++)
            {
                memset(vis,0,sizeof(vis));
                ans+=dfs(i);
            }
            if(ans==p)
            printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

COURSES

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17166   Accepted: 6748

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

Input

Your program should read sets of data from the std input. The first line of the input 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抣l 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.

Output

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.

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

field=source&key=Southeastern+Europe+2000" style="text-decoration:none">Southeastern Europe 2000

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-08-03 10:36:07

POJ1469_COURSES(二部图最大匹配)的相关文章

POJ1469_COURSES(二分图最大匹配)

解题报告 题目传送门 题意: n个学生p门课程,每个学生学习0或1以上的课程. 问:是否可以组成委员会,满足 每个学生代表一门不同的课程 一门课程在委员会中有一名代表 思路: 很明显的二分图的完备匹配. #include <map> #include <queue> #include <vector> #include <cstdio> #include <cstring> #include <iostream> #define N

hdu 5093 Battle ships (二部图+最大匹配)

Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 172    Accepted Submission(s): 84 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible o

ZOJ1654.Place the Robots放置机器人——二部图最大匹配(hungary算法)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 题目描述: Robert 是一个著名的工程师.一天,他的老板给他分配了一个任务.任务的背景是:给定一 个m×n 大小的地图,地图由方格组成,在地图中有3 种方格-墙.草地和空地,他的老板希望 能在地图中放置尽可能多的机器人.每个机器人都配备了激光枪,可以同时向四个方向(上.下. 左.右)开枪.机器人一直待在最初始放置的方格处,不可移动,然后一直朝四个方向开枪.激 光枪发射

POJ2239 Selecting Courses【二部图最大匹配】

主题链接: http://poj.org/problem?id=2239 题目大意: 学校总共同拥有N门课程,而且学校规定每天上12节可,一周上7天. 给你每门课每周上的次数,和哪一天哪一节 课上的.假设有多门课程在同一天同一节课上.那么你仅仅能选择当中一门.那么问题来了:最多能同一时候选多少 门课而不发生冲突呢. 输入说明: 先给你一个N.表示有N门课.接下来N行,每行第一个数字x,表示这门课每周上几节.接下来是x对数.第 一个数D表示是这一周哪一天上的,第二个数C表示是这一天哪一节课上的.

最大匹配-标准-hdoj-2063

过山车 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐.但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或PQK做partner,Grass只愿意和linle或LL做partner,PrincessSnow愿意和水域浪子或伪酷儿做partner.考虑到经费问题,boss刘决定只让找到partner的人

匈牙利算法

(一)首先明确匈牙利算法是干嘛滴? 匈牙利算法是解决二部图最大匹配问题滴. (二)算法的核心思想:不断寻找增广路径,每找到一条增广路径,就通过异或操作使匹配边数加一,直到找不到增广路径,算法结束. (三)算法的基本步骤: (1)任取二部图G(X,Y)的匹配M,若M饱和X,则停止.若M不能饱和X,则取X的未标记的M非饱和点x.(标记的点表示经过此点不存在增广路)令S={x},T= ?.(T集合中的点表示N(S)中已经加入增广路的点)(当不存非饱和点或者所有非饱和点都被标记,算法结束) (2)若N(

大数据日知录:架构与算法

大数据丛书 大数据日知录:架构与算法(大数据领域专家力作,专注大数据架构和算法,全面梳理大数据相关技术) 张俊林 著   ISBN 978-7-121-24153-6 2014年9月出版 定价:69.00元 404页 16开 编辑推荐 这是一本心血之作,历时3年,质量上乘. 从架构与算法的角度,比较全面地分门别类梳理了大数据相关技术. 本书内容紧跟技术前沿,讲解深入浅出,适合大数据领域所有技术人员. 书中还列有作者优选的高质量文献,能为读者节省选择的时间,绝对值得一读. 内容提要 大数据是当前最

二分图学习

图论中的难见就是建图然后套用算法. 特点: 仅仅能一一相应,即XX仅仅能有一个人. 先来一个比較好的入门资料二分图最大匹配 參考 二分图建图方法 算法的思路是不停的找增广路径, 并添加匹配的个数,增广路径顾名思义是指一条能够使匹配数变多的路径,在匹配问题中,增广路径的表现形式是一条"交错路径",也就是说这条由图的边组成的路径. 它的第一条边是眼下还没有參与匹配的,第二条边參与了匹配.第三条边没有..最后一条边没有參与匹配,而且始点和终点还没有被选择过.这样交错进行,显然他有奇数条边.

《大数据日知录:架构与算法》试读

时代背景 记得CSDN之前有篇文章描写叙述了大数据成功预測了美国大选,"大数据"并不真正关心谁来当选下一届美国总统.只是全部的数据都显示:政治科学家和其它人相关人士都觉得奥巴马获得连任可能性比較大.本次的成功预言,展示了大数据强大的能量. 众所周知.企业数据本身就蕴藏着价值.可是将实用的数据与没有价值的数据进行区分看起来可能是一个棘手的问题. 显然,您所掌握的人员情况.工资表和客户记录对于企业的运转至关重要.可是其它数据也拥有转化为价值的力量.一段记录人们怎样在您的商店浏览购物的视频.