Title Link: http://acm.split.hdu.edu.cn/showproblem.php?pid=1083
Time limit:20000/10000 MS (java/others) Memory limit:65536/32768 K (java/others)
Problem Descriptionconsider 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
Your 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 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, 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 the program input and Output:sample Input23 1 2 1 1 1 1 Sample outputyesno Test Instructions: There are n students in the class, there is a total of P class, give the number and number of all students in the PI course, and now form the Class committee, ask each class to have one person as the class representative, and a person can not be the representative of the class, ask whether can set up to meet the requirements of the Class Committee. The main point: Obviously, the P-gate class as the U set, n students as a V set, according to the class situation to get a binary map, if the maximum number of matches is P, you can form a class committee, or will not be formed. AC Code:
1#include <cstdio>2#include <cstring>3#include <vector>4 #defineMAX 4105 using namespacestd;6 intP,n;7 structedge{8 intu,v;9 };TenVector<edge>E; Onevector<int>G[max]; A intMatching[max]; - intVis[max]; - voidInitintLintR) the { - e.clear (); - for(inti=l;i<=r;i++) g[i].clear (); - } + voidAdd_edge (intUintv) - { + E.push_back (Edge) {u,v}); A E.push_back (Edge) {v,u}); at int_size=e.size (); -G[u].push_back (_size-2); -G[v].push_back (_size-1); - } - BOOLDfsintu) - { in for(intI=0, _size=g[u].size (); i<_size;i++) - { to intv=e[g[u][i]].v; + if(!Vis[v]) - { thevis[v]=1; * if(!matching[v] | |DFS (Matching[v])) $ {Panax Notoginsengmatching[v]=u; -matching[u]=v; the return 1; + } A } the } + return false; - } $ intHungarian () $ { - intret=0; -memset (Matching,0,sizeof(matching)); the for(intI=1; i<=p;i++) - {Wuyi if(!Matching[i]) the { -memset (Vis,0,sizeof(Vis)); Wu if(Dfs (i)) ret++; - } About } $ returnret; - } - intMain () - { A intT; +scanf"%d",&t); the while(t--) - { $scanf"%d%d",&p,&n); theInit1, p+n); the for(intI=1, cnt,stu;i<=p;i++) the { thescanf"%d",&CNT); - for(intj=1; j<=cnt;j++) in { thescanf"%d",&Stu); theAdd_edge (i,stu+p); About } the } the if(Hungarian () ==p) printf ("yes\n"); the Elseprintf"no\n"); + } -}
HDU 1083-courses-[Hungarian algorithm template title]