Courses(二分图水题)
来源:互联网 发布:男装批发软件 编辑:程序博客网 时间:2024/06/15 23:11
Courses
Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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:
. 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
23 33 1 2 32 1 21 13 32 1 32 1 31 1
Sample Output
YESNO
题意:有P门课,N个学生,求能否求出一个组满足:每门课只能对应一个人,但是单个人可以对应多门课 且人数(or课程)等于P。
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>using namespace std;int map[502][502];bool visited[502]; //标记男生是否被访问int match[502]; //女生和男生的匹配情况int n,m;bool find(int i) //查找当前的i是否可以匹配{ int j; for(j=1;j<=n;j++) { if(map[i][j]&&!visited[j])//在这里很有必要说一下visited这个数组,因为在之后的递归中,就会把这个标记好了的对象默认为 //已经和上一次的对象匹配过了,这样就不会再访问这个对象了,这在好几个的连续递归中显得尤为重要 { visited[j]=1; if(match[j]==-1||find(match[j])) //在此次查找的时候其实如果已经匹配过了则会在调用的时候即使是已经匹配成功了,由于 //当时在匹配的时候已经对可以匹配成功的做了match的标记了,就会继续查找他的下一个 //能够匹配的对象 { match[j]=i; return 1; } } } return 0;}int main(){ int k,x,y,ans,tmp; int T; scanf("%d",&T); while(T--) { ans=0; memset(map,0,sizeof(map)); memset(match,-1,sizeof(match)); scanf("%d%d",&m,&n); for(int i=1;i<=m;i++)//对有意思的进行初始化 { scanf("%d",&tmp); for(int j=0;j<tmp;j++){ scanf("%d",&y); map[i][y]=1; } } for(int i=1;i<=m;i++) { memset(visited,0,sizeof(visited));//开始标记为全部没有访问 if(find(i)) //查找当前的i是否可以匹配成功 ans++; } printf("%s\n",ans>=m ? "YES" : "NO"); } return 0;}
0 0
- Courses(二分图水题)
- COURSES(二分图)
- hdu1083 Courses(二分匹配)
- poj1469 COURSES(二分图)
- poj1469--COURSES(二分图最大匹配)
- HDU 1083 Courses (二分最大匹配)
- hdu 1083 Courses (二分图匹配)
- COURSES(poj1469,二分图最大匹配)
- hdu 1083 Courses (简单二分匹配)
- poj 1469 COURSES(二分匹配模板)
- hdu 1083 Courses(二分最大匹配)
- poj 1469 COURSES (二分图匹配)
- Courses(二分图完全匹配)
- POJ 1469 COURSES(二分图匹配)
- HDU 1083 Courses(二分图匹配)
- POJ 1469 COURSES(二分匹配-hungary)
- HDU 1083 Courses (二分图)
- hdu2063 过山车&&poj1469 COURSES(二分匹配)
- 安装cocopods可视化插件时出现的问题
- Filter统一编码实现
- 测评后返回结果
- 一个网站重要的在于细节,我们要学会耐心的去完善,去更改
- 面向项目(六)—— 错误(异常)信息的书写
- Courses(二分图水题)
- 网络HTTP、TCP、UDP、Socket 知识总结
- 《关于SDL中的色彩键控》
- poj2553——The Bottom of a Graph(强连通分量)
- HDU1106 排序
- 《java入门第一季》之面向对象(包概述)
- 并查集初学(1)
- 那一年, 我第一次听说华为
- java后台数据库同步使用map进行缓存