Poj 1469 courses

Source: Internet
Author: User
Courses
Time limit:1000 ms   Memory limit:10000 K
Total submissions:17443   Accepted:6873

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 shocould 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 must 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 shoshould 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



Analysis:

We still use binary matching, but we started to use CIN for a long time. We also kept wa without initialization. It's a pain point.


#include<stdio.h>#include<iostream>#include<algorithm>using namespace std;#define M 305int p,n,a[M];int map[M][M],flag[M],pre[M];int find(int x){  for(int i=1;i<=n;i++)  {    if(map[x][i]==1 && flag[i]==0){ flag[i]=1; if(pre[i]==0 || find(pre[i])) { pre[i]=x; return 1; }}  }  return 0;}int main (){  int T;  int i,j,k;  scanf("%d",&T);  while(T--)  {   memset(map,0,sizeof(map));   memset(pre,0,sizeof(pre));   scanf("%d%d",&p,&n);   for(i=1;i<=p;i++)   {     scanf("%d",&k); for(j=1;j<=k;j++) { scanf("%d",&a[j]); map[i][a[j]]=1; }   }   int sum=0;    for(i=1;i<=p;i++)   {       memset(flag,0,sizeof(flag));   if(find(i)) sum++;   }   //cout<<sum<<endl;   if(sum==p)   printf("YES\n");   else    printf("NO\n");  } return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.