Zju1140 Courses 课程

发布时间 2023-10-29 09:45:03作者: 我微笑不代表我快乐

 

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

给出课程的总数P(1<=p<100),学生的总数N(1<=N<=300) 每个学生可能选了一门课程,也有可能多门,也有可能没有. 要求选出P个学生来组成一个协会,每个学生代表一门课程, 且每门课程都有一个学生来代表它。

Format
Input
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.

先是测试数据的个数。

再是P,N

下列P行,代表从第一门课到第P门课程的学生选修的情况。先给出有多少人选修,

再是这些人的学号.

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.

可否组成一个这样的协会

 

#include<bits/stdc++.h>
using namespace std;
const int N=40010;
int T,n1,n2,m,a,b,ans,mch[N];
bool st[N];
vector<int>v[N];
bool find(int x)
{
	for(int i=0;i<v[x].size();i++)
	//枚举所有与x相连的边
	{
		int y=v[x][i];
		if(!st[y])
		{
			st[y]=true;
			if(!mch[y]||find(mch[y]))
			{
				mch[y]=x;
				return true;
			}
		}
	}
	return false;
}
int main(){
	scanf("%d",&T);
	while(T--){
		ans=0;
		memset(mch,0,sizeof(mch));
		scanf("%d%d",&n1,&n2);
		//n1代表课程数,n2代表学生人数
		for(int i=1;i<=n1;i++)
		//对于每个课程,分别有哪些学生选择了它
		{
			v[i].clear();
			scanf("%d",&a);
			for(int j=1;j<=a;j++)
			{
				scanf("%d",&b);
				v[i].push_back(b);
			}
		}
		for(int i=1;i<=n1;i++)
		//对于每个课程找出一条增长路出来
		{
			memset(st,false,sizeof(st));
			if(find(i)) 
				 ans++;
		}
		puts(ans==n1?"YES":"NO");
	}
	return 0;
}