HDU 1083 Courses "exact Match"

Source: Internet
Author: User
Courses Time limit:20000/10000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3805 Accepted Submission (s): 1815


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 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 program input and output:
Sample Input
2 3 3 3 1 2 3 2 1 2 1 1 3 3 2 1 3 2 1 3 1 1
Sample Output
YES NO/*:
The exact matching problem is obtained by the Hungarian algorithm.
*/
Given a binary graph G, in a sub-figure m of G, any two edges of M's edge set are not attached to the same vertex, then M is a match.
Choosing the largest subset of such edges is called the maximum matching problem for graphs (maximal matching problem)
If one match, each vertex in the diagram is associated with an edge in the graph, it is said to be an exact match, also known as a complete match.
The maximum matching of binary graphs can be obtained by the maximum flow or Hungarian algorithm.

#include <cstdio> #include <cstring> int n,m,p,n;
int map[1010][1010],vis[1010],st[1010];
    BOOL Find (int x) {int J;
            for (j=1; j<=n; J + +) {if (map[x][j]==true&&vis[j]==false) {vis[j]=1; if (st[j]==0| |
                Find (St[j])) {st[j]=x;
            return true;
}}} return false;
    } int main () {int t,i,j,a,b,ans;
    scanf ("%d", &t);
        while (t--) {memset (map,0,sizeof (map));
        memset (st,0,sizeof (ST));
        scanf ("%d%d", &p,&n);
            for (I=1; i<=p; i++) {scanf ("%d", &a);
                for (j=1; j<=a; J + +) {scanf ("%d", &b);
            Map[i][b]=1;
            }} for (i=1,ans=0; i<=p; i++) {memset (vis,0,sizeof (VIS));
        if (find (i)) ans++;
        } if (ans==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.