杭电oj-1083 C
来源:互联网 发布:剑三纯阳捏脸数据成男 编辑:程序博客网 时间:2024/06/10 07:52
C - Courses
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:
Input
2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1
Output
YES
NO
单纯的二分匹配
//单纯的二分匹配#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;const int N = 420;int p,n,match[N],used[N];vector<int> G[N];void add_edge(int a,int b){ G[a].push_back(b); G[b].push_back(a);}bool dfs(int a){ used[a]=1; for(int i=0;i<G[a].size();i++) { int &e=G[a][i];int w=match[e]; if(w<0||!used[w]&&dfs(w)) { match[e]=a; match[a]=e; return true; } } return false;}bool bine(){ memset(match,-1,sizeof(match)); for(int i=1;i<=p;i++) { if(match[i]<0) { memset(used,0,sizeof(used)); if(!dfs(i)) return false; } } return true;}int main(){ int t;cin>>t; while(t--) { cin>>p>>n;int c,d; for(int i=0;i<=n+p;i++) { G[i].clear(); } for(int i=1;i<=p;i++) { cin>>c; for(int j=0;j<c;j++) { cin>>d; add_edge(i,p+d); } } if(bine()) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
- 杭电oj-1083 C
- 杭电oj 1005 c语言实现
- 杭电oj分类
- 杭电oj
- 杭电OJ 2054
- 杭电OJ 1028
- 杭电 oj 1001
- 杭电OJ Elevator
- 杭电oj 1009
- 杭电oj-1003
- 杭电oj-1004
- 杭电oj-1005
- 饭卡 杭电oj
- 杭电OJ分类
- 杭电OJ 1002
- 杭电OJ 1001
- 杭电OJ 1003
- 杭电OJ 1004
- 适配器模式
- Desert King POJ
- POJ 3692 Kindergarten(二分图最大团)
- 前端基础面试题
- stm32-定时器-脉冲捕获功能
- 杭电oj-1083 C
- FFMPEG音视频解码
- 笔试10.12
- sdut 1479 数据结构实验之栈与队列九:行编辑器
- 练习2
- MySQL学习笔记(1)--mysql 数据类型
- ARM异常处理方式简单介绍
- <LeetCode>557. Reverse Words in a String III
- HDU2296(AC自动机+DP)