Courses

                                                                                                    Time Limit: 20000/10000 MS (Java/Others)   

                                                                                                     Memory Limit: 65536/32768
K (Java/Others)



                                                               -> Link <-

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

题目废话了一大堆看了好久,重点就那么几句;

题意:P门课程,n个同学,只要某位同学 visited 过一门课程,那么这位同学就可以是这门课程的代表;求一个集合,满足每门课程都有一个代表,且一位同学只能代表一门课程;

输入:P 课程数目,n 学生数目;接下来P行每行先有一个cout表示此门课程有 cout 位同学 visited 过;

输出:是否能找到这样的一个匹配,是则YES,反之NO\n;

典型的二分图最大匹配,有点像完全匹配,只要判断找到最大匹配是否等于P即可;数据范围都很小,所以用匈牙利算法邻接矩阵实现:

using namespace std;
const int N=300+10;
int p,n,g[N][N],linked[N],v[N];
int dfs(int u)
{
for(int i=1; i<=n; i++)//模板;
if(!v[i]&&g[u][i])
{
v[i]=1;
if(linked[i]==-1||dfs(linked[i]))
{
linked[i]=u;
return 1;
}
}
return 0;
}
void hungary()
{
int num=0,i;
memset(linked,-1,sizeof(linked));
for(i=1; i<=p; i++)
{
memset(v,0,sizeof(v));//注意这里每次都要清楚标记;
if(dfs(i)) num++;
}
if(num==p) printf("YES\n");
else printf("NO\n");
}
int main()
{
int t,i,j,x;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&p,&n);
memset(g,0,sizeof(g));
for(i=1; i<=p; i++)
{
scanf("%d",&x);
while(x--)
{
scanf("%d",&j);
g[i][j]=1;
}
}
if(p>n)//此条件很容易得出;
{
printf("NO\n");
continue;
}
hungary();
}
return 0;
}

HDU-1083Courses,二分图模板题!的更多相关文章

  1. POJ 3041 Asteroids(二分图模板题)

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  2. HDU 2138 Miller-Rabin 模板题

    求素数个数. /** @Date : 2017-09-18 23:05:15 * @FileName: HDU 2138 miller-rabin 模板.cpp * @Platform: Window ...

  3. HDU 1392 凸包模板题,求凸包周长

    1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...

  4. HDU 2586 (LCA模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 /   \ 2      3 ...

  5. HDU 2082 母函数模板题

    找单词 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  6. HDU 2087 kmp模板题

    s为主串 t为模板串 求t的nextt 加const #include<stdio.h> #include<string.h> #include<algorithm> ...

  7. 【网络流#3】hdu 1532 - Dinic模板题

    输入为m,n表示m条边,n个结点 记下来m行,每行三个数,x,y,c表示x到y的边流量最大为c 这道题的模板来自于网络 http://blog.csdn.net/sprintfwater/articl ...

  8. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  9. 最长回文 HDU - 3068 manacher 模板题

    题意:找串的最长回文字串(连续) 题解:manacher版题 一些理解:首位加上任意两个字符是为了判断边界. 本算法主要是为了 1.省去奇偶分类讨论. 2.防止形如aaaaaaa的串使得暴力算法蜕化为 ...

随机推荐

  1. Visual studio docker build no such file or directory

    在我构建新的镜像的时候, 发生 了  no such file or directory 的错误.  这个错误找了半天, 没头绪,项目结构是这样的: WebApplication1 建立在根目录下,是 ...

  2. 450 Delete Node in a BST 删除二叉搜索树中的结点

    详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...

  3. Android 使用pl.droidsonroids.gif.GifImageView在安卓中显示动图遇到的问题

    在做一款聊天软件,其中聊天界面需要发送表情,而表情都是动图,在安卓中想要显示动图,就要借助第三方框架,我选的是pl.droidsonroids.gif.GifImageView. 使用方法如下:你在g ...

  4. .NET 原理之 ViewState

    1.从MSDN中我们可以知道一个页面生命周期大约可分为为:页请求.开始.初始化.加载.验证.回发事件处理.呈现.卸载这几个阶段.       HttpHandler是无状态的,aspx是高级的Http ...

  5. Python学习 Day 5 高阶函数 map/reduce filter sorter 返回函数 匿名函数 装饰器 偏函数

    高阶函数Higher-orderfunction 变量可以指向函数 >>> abs #abs(-10)是函数调用,而abs是函数本身 <built-in function ab ...

  6. [Tunny]CSS LESS框架基础

    [黄映焜/Tunny,20140711] Less 是一个Css 预编译器,意思指的是它可以扩展Css语言,添加功能如允许变量(variables),混合(mixins),函数(functions) ...

  7. 高仿人人网客户端Android版项目源码

    高仿人人网客户端,有兴趣的盆友可以研究下,里面主要包含的一些UI设计与交互.(注:项目中有少许问题,apk能运行,希望开发者可以参考代码研究一下.) 源码下载:http://code.662p.com ...

  8. PHP运算符考察点

    PHP运算符优先级 运算符优先级指定了两个表达式绑定得有多"紧密".例如,表达式 1 + 5 * 3 的结果是 16 而不是 18 是因为乘号(*)的优先级比加号(+)高.必要时可 ...

  9. Shiro的subject实质上是当前执行用户的特定视图。

    Shiro的subject实质上是当前执行用户的特定视图. 通过org.apache.shiro.SecurityUtils可以查询当前执行用户: Subject currentUser = Secu ...

  10. hibernate fetch属性

    fetch的属性值有:select(默认值).join.subselect 1)当fetch=”select”时,程序会先查询返回要查询的主体对象,然后根据lazy属性看是否懒加载. 2)当fetch ...