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. how-to-fix-fs-re-evaluating-native-module-sources-is-not-supported-graceful

    http://stackoverflow.com/questions/37346512/how-to-fix-fs-re-evaluating-native-module-sources-is-not ...

  2. 加密解密(3)Bob到CA申请证书过程

    网络安全中最知名的人物大概就是Bob和Alice了,因为很多安全原理阐述中都用这两个虚拟人物来进行实例说明. 我们来看看Bob是怎么从CA中心获得一个数字证书的: 1.Bob首先创建他自己的密钥对(k ...

  3. 关于min-height:100%的解决办法

    前几天碰到一个问题,在用bs和jq2.2.0开发时,min-height设为100%在firefox和ie下没有起作用,先用css改了一下,但是min-height虽然是奏效了,但同时出现了其他css ...

  4. 安装CentOS--设置网络_2

    (1)虽然CentOS 7已经可以联网,但是在日常的运维工作中,我们是需要手动给Linux系统设置IP地址的.输入如下命令. # vi /etc/sysconfig/network-scripts/i ...

  5. java获取公网ip以及物理地址和代理商

    package ipconfig; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...

  6. [BZOJ1009][HNOI2008]GT考试 DP+矩阵快速幂+KMP

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1009 我们令$dp(i,j)$表示已经填了$i$位,而且后缀与不幸运数字匹配了$j$位,那 ...

  7. React 实践心得:react-redux 之 connect 方法详解

    Redux 是「React 全家桶」中极为重要的一员,它试图为 React 应用提供「可预测化的状态管理」机制. Redux 本身足够简单,除了 React,它还能够支持其他界面框架.所以如果要将 R ...

  8. 最新最强短视频SDK——来自RDSDK.COM

    北京锐动天地信息技术有限公司成立于2007年9月.多年来一直专注于音视频领域核心技术的研发, 拥有Windows.iOS.Android全平台自主知识产权的领先技术产品. 2011年获得新浪战略投资, ...

  9. R in action读书笔记(14)第十一章 中级绘图 之一:散点图(高能预警)

    第十一章中级绘图 本章内容: 二元变量和多元变量关系的可视化 绘制散点图和折线图 理解相关图 学习马赛克图和关联图 本章用到的函数有: plot hexbin ablines iplot scatte ...

  10. 阿里云CDN服务功能介绍