POJ 1469 COURSES (Hungarian algorithm template)

Source: Internet
Author: User

http://poj.org/problem?id=1469

COURSES
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 19419 Accepted: 7642

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 was possible to form a committee of exactly P students that satisfies simultaneously t He conditions: 
    • Every student in the Committee represents a different course (a student can represent a course if he/she visits that cours e)
    • Each course have 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 was presented in the following format:
P NCount1 Student1 1Student1 2... Student1 Count1Count2 Student2 1Student2 2... Student2 Count2...COUNTP STUDENTP 1 studentp 2  STUDENTP countp 
the first line of each data set contains the positive intege RS separated by one blank:p (1 <= P <=)-The number of courses and N (1 <= n <=)-The number of Stu Dents. 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 was a line this starts with an integer Count I (0 <= Count i <= N) representing the Numbe R of Students visiting course I. Next, after a blank, you basis l FIND the Count I students, visiting the course, each of the consecutive separated by one blank. Students is numbered with the positive integers from 1 to n.  there is no blank Lines between consecutive sets of data. Input data is correct. &NBSP;

Output

The result of the program is in the standard output. For each input data set, prints on a, "YES" if it is possible to form a committee and "NO" otherwise . There should not being 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


Main topic:

A total of n students with P-gate course, a student can choose any one

Door or multiple courses, ask whether to achieve:

1. Each student is selected for a different class (i.e. no two students can choose the same course)

2. Each course has a representative (that is, p-courses have been successfully selected)

Enter as:

P N (number of courses and number of students)

Then there are p lines, formatted as Count Studenti studenti+1 ... studentcount

(count indicates the number of students interested in course 1, followed by count students)

If the first line 3 1 2 3 indicates that the student 1 and the Student 2 and the Student 3 is interested in the course 1

The output is:

If you can meet the above two requirements this output "YES", otherwise "NO"

#include <stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#defineINF 0x3f3f3f3f#defineN 310intG[n][n], vis[n], used[n];intn, p;BOOLFind (intu) {    inti;  for(i =1; I <= N; i++)    {        if(!vis[i] &&G[u][i]) {Vis[i]=1; if(!used[i] | |Find (Used[i])) {Used[i]=u; return true; }        }    }    return false;}intMain () {intA, B, I, ans, t; scanf ("%d", &t);  while(t--) {scanf ("%d%d", &p, &N); memset (G,0,sizeof(G)); Ans=0;  for(i =1; I <= p; i++) {scanf ("%d", &a);  while(a--) {scanf ("%d", &b); G[I][B]=1; }} memset (used,0,sizeof(used));  for(i =1; I <= p; i++) {memset (Vis,0,sizeof(VIS)); if(Find (i)) ans++; }        if(ans = =p) printf ("yes\n"); Elseprintf ("no\n"); }    return 0;}

POJ 1469 COURSES (Hungarian algorithm template)

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.