hdoj1083-courses (binary graph matching (Hungarian algorithm))

Source: Internet
Author: User

Problem 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 CommitteeYour program should read sets of the data from a text file. The first line of the input file contains the number of the data sets. Each data set was presented in the following format:P NCount1 Student1 1 Student1 2 ... Student1 Count1Count2 Student2 1 Student2 2 ... Student2 Count2......COUNTP STUDENTP 1 studentp 2 ... STUDENTP COUNTPThe first line, each data set contains, positive integers separated by one blank:p (1 <= P <=)-The Numbe R of courses and N (1 <= n <=)-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 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'll 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. 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. An example of program input and output:

Sample Input

23 33 1 2 32 1 21 13 32 1 32 1 31 1

Sample Output

Code:

1#include <bits/stdc++.h>2 using namespacestd;3 4 #defineCLR (x, v) memset (x, V, sizeof (x))5 Static Const intMAXN =1005;6 7 BOOLCOURSE[MAXN][MAXN];8 BOOLVIS[MAXN];9 intLINK[MAXN];Ten intp, N; One intT; A  - BOOLFind (intx) - { the      for(inti =1; I <= N; ++i) -     { -         if(!vis[i] &&Course[x][i]) -         { +Vis[i] =1; -             if(!link[i] | |Find (Link[i])) +             { ALink[i] =x; at                 return 1; -             } -         } -     } -     return 0; - } in  - intMain () to { +scanf"%d", &t); -      while(t--) the     { *CLR (Course,0); $CLR (Vis,0);Panax NotoginsengThe CLR (link,0); -  thescanf"%d%d", &p, &n); +          for(inti =1; I <= p; ++i) A         { the             intm; +scanf"%d", &m); -              while(m--) $             { $                 intx; -scanf"%d", &x); -COURSE[I][X] =1; the             } -         }Wuyi  the         intsum =0; -  Wu          for(inti =1; I <= p; ++i) -         { AboutCLR (Vis,0); $             if(Find (i)) -++sum; -         } -  A         if(Sum = =p) +Puts"YES"); the         Else -Puts"NO"); $     } the}
View Code

hdoj1083-courses (binary graph matching (Hungarian algorithm))

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.