The first question of winter vacation .. Water question Hungary cut for 1 hour .. Potholes

Source: Internet
Author: User

// Simple HungaryAlgorithmTemplate question .. In this example, cows have the largest production capacity in their favorite columns, including n and M. Calculate the maximum output.
// If MK [I] = 0, it indicates that no access has been made. If it is set to 1, it indicates that no access has been made.
# Include <iostream>
# Include <stdio. h>
# Include <string. h>
Using namespace STD;
Const int maxn = 500;
Int MK [maxn];
Int NX, NY; // Number of vertices in the X and Y Sets
Int G [maxn] [maxn]; // adjacent matrix, edge information between vertices in X and Y Sets
Int CX [maxn], CY [maxn];
Int path (int u)
{
For (INT v = 1; v <= NY; V ++) // consider all Yi vertices v
{
If (G [u] [v] &! MK [v])
{
MK [v] = 1;

If (CY [v] =-1 | path (CY [v])
{
CX [u] = V; // match V to u
CY [v] = u; // match u to V
Return 1; // find the augmented path
}
}
}
Return 0; // if there is no augmented path from u
}
Int maxmatch () // calculate the Hungarian algorithm for maximum matching of Two Graphs
{
Int res = 0;
Int I;
Memset (CX, 0xff, sizeof (CX ));
Memset (CY, 0xff, sizeof (CY ));
For (I = 1; I <= NX; I ++)
{
If (CX [I] =-1) // search for augmented path from each uncovered point
{
Memset (MK, 0, sizeof (MK ));
Res + = path (I); // each time an augmented path is found, the matching number plus 1 is allowed.
}
}
Return res;
}
Int main ()
{
Int n, m;
Int I, J;
Int num, ans;
While (scanf ("% d", & N, & M )! = EOF)
{
Nx = N; ny = m;
Memset (G, 0, sizeof (g ));
For (I = 1; I <= N; I ++)
{
Scanf ("% d", & num );
For (j = 1; j <= num; j ++)
{
Scanf ("% d", & ANS );
G [I] [ANS] = 1;
}
}
Printf ("% d \ n", maxmatch ());
}
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.