博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[poj 1469]Courses
阅读量:4611 次
发布时间:2019-06-09

本文共 3203 字,大约阅读时间需要 10 分钟。

COURSES
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23592   Accepted: 9238

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

23 33 1 2 32 1 21 13 32 1 32 1 31 1

Sample Output

YESNO 题目大意 有一些课程,课程有一些同学参加。现在给每个课程匹配一个代表,使得每个课程都有一个代表,每个学生都代表一个课程(没有重复的)。 问是否能够完全匹配。 思路 将所有课程与学生进行匹配 二分匹配啊 用匈牙利算法。之前已经普及过了
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 8 using namespace std; 9 10 int read()11 {12 char ch=getchar();13 int x=0,f=1;14 while(ch<'0' || ch>'9'){ if(ch=='-')f=-1;ch=getchar();}15 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}16 return x*f;17 }18 int n,c,p,tmp,tmp2,con[301],ans;19 bool g[301][301],used[301];20 bool dfs(int u)21 {22 for(int v=1;v<=p;v++)23 {24 if(g[u][v]==1 && !used[v])25 {26 used[v]=1;27 if(con[v]==-1 || dfs(con[v]))28 {29 con[v]=u;30 return true;31 }32 }33 }34 return false;35 }36 int hungary()37 {38 int res=0;39 memset(con,-1,sizeof(con));40 for(int j=1;j<=c;j++)41 {42 memset(used,0,sizeof(used));43 if(dfs(j))res++;44 }45 return res;46 }47 int main()48 {49 n=read();50 for(int i=1;i<=n;i++)51 { 52 memset(g,0,sizeof(g));53 c=read();p=read();54 for(int j=1;j<=c;j++)55 {56 tmp=read();57 for(int k=1;k<=tmp;k++)58 {59 tmp2=read();60 g[j][tmp2]=1;61 }62 }63 ans=hungary();64 if(ans==c)printf("YES\n");65 else printf("NO\n");66 }67 return 0;
View Code
 

 

 

转载于:https://www.cnblogs.com/taojy/p/7182552.html

你可能感兴趣的文章
(转载) Android Studio你不知道的调试技巧
查看>>
队列实现霍夫曼树
查看>>
JAVA 笔记(一)
查看>>
{Nodejs} request URL 中文乱码
查看>>
异常及日志使用与项目打包
查看>>
数组相关函数
查看>>
Python 和其他编程语言数据类型的比较
查看>>
T2695 桶哥的问题——送桶 题解
查看>>
HTML5 表单
查看>>
Android群英传》读书笔记 (3) 第六章 Android绘图机制与处理技巧 + 第七章 Android动画机制与使用技巧...
查看>>
关于微信公众平台测试号配置失败的问题
查看>>
【NOIP2001】统计单词个数
查看>>
linux常用端口
查看>>
异常处理
查看>>
/proc/uptime详解
查看>>
如何建立合适的索引?
查看>>
acwing 651. 逛画展
查看>>
Vijos P1243 生产产品 (单调队列优化DP)
查看>>
iOS常用第三方库 -转
查看>>
Android布局学习
查看>>