Hdoj 2063 roller Coaster (the Hungarian algorithm of the binary map matching)

Source: Internet
Author: User

Roller Coaster http://acm.hdu.edu.cn/showproblem.php?pid=2063
Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 13019 Accepted Submission (s): 5709


problem DescriptionRPG Girls today and we go to the amusement park to play, finally can sit on the dream of the roller coaster. However, the roller coaster in each row only two seats, and there is an unwritten rule, is that every girl must find all the boys do partner and her sitting. However, each girl has their own ideas, for example, rabbit only willing and xhd or pqk do Partner,grass only willing and linle or ll do partner,princesssnow willing and water prodigal or pseudo-queer do partner. Considering the financial problems, boss Liu decided to only find partner people to ride the roller coaster, other people, hey, just stand down and watch it. Smart Acmer, can you help calculate the maximum number of pairs can be on the roller coaster?  
Inputthe first line of input data is three integers k, M, N, respectively, indicating the number of possible combinations, the number of females, and the number of males. 0<k<=1000
1<=n and m<=500. The next K-line, each line has two numbers, respectively, the female AI is willing to and boys BJ do partner. Last 0 end input.  
Outputfor each set of data, output an integer that represents the maximum number of combinations that can be seated on a roller coaster.  
Sample Input
6 3 31 11 21 32 12 33 10
 
Sample Output
3
 
AuthorPrincesssnow 
SourceRPG Training Tournament 
recommendLCY | We have carefully selected several similar problems for you:1068 1083 2444 1281 1150 //Two map matching Hungarian algorithm

/*

Dichotomy: A binary graph is a graph whose vertices can be categorized into two sets X and Y, all of the two vertices of the border are exactly one of the set X, and the other belongs to set Y.

Binary graph matching: 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.
Maximum match: The match that contains the most number of edges in the graph is called the maximum match of the graph.
Perfect match: If all the points are on the matching edge, the maximum match is said to be the perfect match.

The basic concept of binary graph matching:

Not covered point
Set VI is a vertex of G, if VI is not associated with any of the edges that belong to the matching m, it is said that VI is an open point.
Staggered Rail
Set P is a track of Figure g, if any of P's two adjacent edges must be one of the M and the other does not belong to M, it is called P is staggered rail.
Augmented orbit (augmented path)
The two endpoints are jagged tracks that are non-covered points, called an augmented orbit.

The properties of an augmented orbit:

The path length of the 1:p must be odd, and the last edge of the first Bien Hoa does not belong to M.
2:p can get a larger match m ' by taking the inverse operation.
3:m is the maximum match for G when and only if there is no augmented path relative to M.

Binary graph maximum matching Hungarian algorithm:

The idea of the algorithm is:

constantly looking for augmented orbit, and increase the number of matches, augmented orbit as the name implies is a path that can make the number of matches, in the matching problem, the expression of augmented orbit is a "staggered rail", that is, the path of the edge of the graph, its first edge is currently not involved in matching, The second side participates in the match, and the third side does not have one. The last edge does not participate in the match, and the start and end points have not been selected. So staggered, he obviously had odd edges. So for such a path, we can change the first edge to match, and the second edge to an unmatched ... And so on. That is, the "inverse" of all the edges, it is easy to find such a modification, the match is still legal, but the number of matches increased by one pair. In addition, a separate edge that connects two unmatched points is clearly a staggered track. It can be proved that when the augmented orbit is no longer found, A maximum match is obtained. This is the idea of the Hungarian algorithm.

*/


#include <cstdio>

#include <cmath>
#include <cstring>
#include <algorithm>
using namespace Std;
const int m=1010;
int edges,n,m;
int map[m][m];//adjacency Matrix
int vis[m];//n2, the access to each point in the collection is marked
An int match[m];//represents a point in the current N1 that can be adjacent to the N2 collection



void init (int edge)
{
int i;
memset (map,0,sizeof (map));
memset (match,0,sizeof (match));
for (i=1;i<=edge;i++)
{
int x, y;
scanf ("%d%d", &x,&y);
map[x][y]=1;
}
}

//Hungarian algorithm main part
int find (int node) //Whether there is an augmented path starting with node node in the N1 collection
{
int i;
for (i=1;i<=m;i++)
if (map[node][i]&&!vis[i])//If node I is adjacent and has not been accessed
{
Vis[i]=1;
if (match[i]==0| | Find (Match[i])//If a non-covered point I is found or a node adjacent to I has an augmented path
{
Match[i]=node;
return 1;
}
}
return 0;
}

int main ()
{
while (scanf ("%d%d%d", &edges,&n,&m), edges)
{
init (edges);
int ans=0,i;
for (i=1;i<=n;i++)
{
memset (vis,0,sizeof (VIS));
if (find (i))
ans++;
}

printf ("%d\n", ans);
}
return 0;
}

Hdoj 2063 roller Coaster (the Hungarian algorithm of the binary map matching)

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.