Poj courses (Binary match)

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 1469

Courses
Time limit:1000 ms   Memory limit:10000 K
Total submissions:17135   Accepted:6730

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

Source

Southeastern Europe 2000

Question: This is a p course. N students are asked if p students can be selected to take different courses for each student, and each course has one student. For a typical bipartite graph matching problem, we only need to calculate the maximum number of Bipartite Graph Matching. If the number is the same as the number of courses, yes is output; otherwise, no is output.

The Code is as follows:

# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> using namespace STD; /*************************************** * ********************************** // Bipartite Graph matching (dfs implementation of the Hungary algorithm) // initialization: Division of vertices on both sides of G [] [] // establish G [I] [J] to indicate directed edge of I-> J, it is the matching on the left to the right. If no edge is connected, the value 0 is initialized. // L is the number of vertices on the left to match, and r is the number of vertices on the right to match. // call: res = Hungary (); Maximum number of matched outputs // advantage: Suitable for dense graphs, DFS find augmented paths, simple and easy to understand // time complexity: O (VE) //************************************** *************** * ******************* // The vertex number starts from 0 # define maxn 317int P, N; // L, r number int K; int G [maxn] [maxn], linker [maxn]; bool used [maxn]; int DFS (int l) // find the augmented path {int R; For (r = 1; r <= N; r ++) from the left. // The vertex number starts from 0, to modify {If (G [l] [r] from 1! = 0 &&! Used [R]) {// find the augmented path, reverse used [R] = true; If (linker [R] =-1 | DFS (linker [R]) {linker [R] = L; return 1 ;}} return 0; // do not forget this, often forget this sentence} int Hungary () {int res = 0; int L; memset (linker,-1, sizeof (linker); For (L = 1; L <= P; l ++) {memset (used, 0, sizeof (used); If (DFS (l )! = 0) RES ++;} return res;} int main () {int I, j, res, R; int t; scanf ("% d", & T ); while (t --) {scanf ("% d", & P, & N); memset (G, 0, sizeof (g); for (I = 1; I <= P; I ++) {scanf ("% d", & K); For (j = 1; j <= K; j ++) {scanf ("% d", & R); G [I] [r] = 1 ;}res = Hungary (); If (RES = P) printf ("Yes \ n"); elseprintf ("NO \ n");} return 0 ;}


Poj courses (Binary match)

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.