POJ1469 COURSES "binary graph max match · HK algorithm "

Source: Internet
Author: User

COURSES
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17777 Accepted: 7007

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 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 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.

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

Source

Southeastern Europe 2000Test Instructions: There are p courses, n students, each class can only correspond to one person, but a single person can correspond to many courses. The maximum match is equal to P. Solution: Hungary can also solve, see the book introduced this HK algorithm, time complexity to lower, so tried the next, but ... It's a lot of trouble to write.


#include <stdio.h> #include <string.h> #include <queue> #define MAXN 305#define MAXP 105#define MAXM MAXN * maxp#define inf 0x3f3f3f3fint HEAD[MAXP], ID, p, N, dis;struct Node {int V, next;}     E[maxm];int Dx[maxp], DY[MAXN], Cx[maxp], cy[maxn];bool visy[maxn];void addedge (int u, int v) {e[id].v = v;    E[id].next = Head[u]; Head[u] = id++;}    void Getmap () {int k, V, i; id = 0;    scanf ("%d%d", &p, &n);    Memset (Head,-1, sizeof (int) * (P + 1));        for (i = 1; I <= p; ++i) {scanf ("%d", &k);            while (k--) {scanf ("%d", &v);        Addedge (i, v);    }}}bool SearchPath () {std::queue<int> Q; int i, u, v;    dis = INF;    memset (DX, 0, sizeof (int) * (P + 1));    memset (dy, 0, sizeof (int) * (n + 1));    for (i = 1; I <= p; ++i) {if (!cx[i]) Q.push (i); } while (! Q.empty ()) {u = Q.front ();        Q.pop ();        if (Dx[u] > dis) break;    for (i = head[u]; I! =-1; i = E[i].next) {        if (!dy[v = e[i].v]) {Dy[v] = Dx[u] + 1;                if (!cy[v]) dis = dy[v];                    else {Dx[cy[v]] = Dy[v] + 1;                Q.push (Cy[v]); }}}}} return dis! = INF;}    int findpath (int u) {int I, V;  for (i = head[u]; i =-1; i = e[i].next) {if (!visy[v = e[i].v] && Dx[u] + 1 = = Dy[v]) {Visy[v]            = 1;            if (dy[v] = = Dis && cy[v]) continue;                if (!cy[v] | | findpath (CY[V])) {Cy[v] = u; cx[u] = v;            return 1; }}} return 0;}    int Maxmatch () {int ans = 0, I;    memset (CX, 0, sizeof (int) * (P + 1));    memset (CY, 0, sizeof (int) * (n + 1));        while (SearchPath ()) {memset (Visy, 0, sizeof (BOOL) * (n + 1));    for (i = 1; I <= p; ++i) if (!cx[i]) ans + = Findpath (i); } return ans; void Solve () {printf (maxmatch () = = P? "yes\n": "no\n");} int main () {// Freopen ("Stdin.txt", "R", stdin);    int t;    scanf ("%d", &t);        while (t--) {getmap ();    Solve (); } return 0;}


POJ1469 COURSES "binary graph max match · HK 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.