Hihocoder #1122: Two-figure binary map the best matching algorithm of Hungary

Source: Internet
Author: User

#1122: The Hungarian algorithm for the maximum matching of Shi Shi in binary graphs time limit:10000mscase time Limit:1000msmemory LIMIT:256MBDescription

The last time we had all the problematic matchmaking tables removed, the next thing to do was arrange a blind date. Because the time of the new year is not very long, so my aunt hopes to be able to arrange a more blind date in one day. Since a person can only have a blind date with one person on the same day, choose as many combinations as possible from the current matchmaking table, and everyone will not appear two times . Do not know there is no good way, for the current given date table, can calculate the maximum number of simultaneous arrangements to arrange a blind date?

In the same way, we first convert the given case table to figure g= (v,e). We already knew in the last time that this figure could be dyed in black and white. It's worth remembering all nodes that represent women as point set a, which indicates that the male node is recorded as Point set B. Then there is a∪b=v. The problem is that the two endpoints of all sides e belong to the AB two sets respectively. It can be represented as the following diagram:

In the same way, we divide all the edges into two sets. Set S and set M, also have s∪m=e. The edge set S represents the blind date to be carried out in this round of matchmaking, and the side set M is not at this time. For any side (u,v) ∈s, we call you and V a set of matches that match each other. In Figure g, we represent the edge set S with a solid line, and the edge set M is represented by a dashed bar. Get:

The original problem is converted to a maximum of how many edges to set S, so that any two edges in the S collection are not contiguous (that is, there are common vertices). Obviously, | s|<=min{| a|, | b|}.

So can you find an algorithm that makes it easy to calculate as many edges as possible into the set S? We might as well take a look at an example:

For the points that have been matched we do not consider, we never match the points to do. Here we select the points (A3 and A4) that have not yet been matched in the A collection to consider:

For A3 points, we can find that A3 is connected to the right side of the B4 and does not match. The (A3,B4) edge is added directly to the set S.

For A4 points, we find that the B3,B4 points connected to the A4 have been matched. However, it can be found that if we connect A2 and B2, we can empty the B3 point. Then you can connect (A2,B2), (A4,B3) at the same time. Turns the original match into two matches.

Let's take a closer look at this step: we mark the associated edges of this transformation, as shown in the Purple 3-edged (A2,B2), (A2,B3), (A4,B3).

These three edges make up a path, and you can see that this path has a very special nature. The dashed and solid lines are interlaced with each other, and the start and end points are not matched, and belong to two different collections. We call this path a staggered path.

Further analysis, for any one staggered path, the number of dashed lines must be 1 more than the number of solid lines. We swapped the dashed lines with the solid lines and became the following figure:

On the basis of the original 1 matches, we got 2 new matches, and the number of s set edges increased by 1. And the original point in the match is still a matching state.

Look back at the A3 Point match: for (A3,B4) This path also satisfies the nature of the interleaved path.

Now we have a valid algorithm for finding a new match:

Select an unmatched point to find out if there is a jagged path starting with it. If present, the edge of the staggered path is exchanged. Otherwise, in the current situation, the point cannot be found to match.

And for points that have already been matched, the algorithm does not change the matching state of a point. So when we calculate all the unmatched points and still have no interleaved paths, we can't find more matches. At this point the number of edges in the S collection is the maximum number of edges, which we call the maximum number of matches.

So let's comb the whole algorithm once again:

1. Enumerate each point I in turn;
2. If point I has not yet been matched, use this point as the starting point to query the interleaved path.

Finally, the maximum number of matches can be obtained.

On this basis there are still two areas that can be optimized:

1. Enumeration of points: When we enumerate the points in all a, we do not need to enumerate the points in B, we have obtained the maximum match.
2. In the process of querying the interleaved path, it is possible that the AI is directly connected to BJ, where BJ is the point that has been matched, and the interleaved path is not found after BJ. After that, an Ai found a staggered path {Ai,bx,ay,..., AZ,BJ} extended to Bj. Since it has been calculated that BJ does not have an interleaved path, there is additional redundancy if you compute it again at this time. So we need to enumerate each AI when the points in the B collection have been queried, and the records need to be emptied when the starting point is different.

Pseudo code

input

Line 1th: 2 positive integers, n,m (N means 2≤n≤1,000,m for number of sides 1≤m≤5,000)
2nd.. M+1 line: Two integers per line u,v, representing an no-edge (U,V)

Output

Line 1th: An integer that represents the maximum number of matches

Sample Input
5 43 21 35 41 5
Sample Output
2

Problem solving: Maximum match

1 /*2 @author: Lev3 @date:4 */5#include <iostream>6#include <cstdio>7#include <cmath>8#include <cstring>9#include <string>Ten#include <cstdlib> One#include <algorithm> A#include <map> -#include <Set> -#include <queue> the#include <climits> -#include <deque> -#include <sstream> -#include <fstream> +#include <bitset> -#include <iomanip> + #defineLL Long Long A #defineINF 0x3f3f3f3f at  - using namespacestd; - Const intMAXN =1010; -vector<int>G[MAXN]; - intLINK[MAXN]; - BOOLUSED[MAXN]; in BOOLDfsintu) { -      for(inti = g[u].size ()-1; I >=0; --i) { to         if(!Used[g[u][i]]) { +Used[g[u][i]] =true; -             if(Link[g[u][i]] = =-1||DFS (Link[g[u][i])) { theLink[g[u][i]] =u; *Link[u] =G[u][i]; $                 return true;Panax Notoginseng             } -         } the     } +     return false; A } the intMain () { +     intn,m; -      while(~SCANF ("%d%d",&n,&m)) { $          for(inti =0; i < MAXN; ++i) { $ g[i].clear (); -Link[i] =-1; -         } the          for(inti =0; I < m; ++i) { -             intu,v;Wuyiscanf"%d%d",&u,&v); the G[u].push_back (v); - g[v].push_back (u); Wu         } -         intAns =0; About          for(inti =1; I <= N; ++i) $         if(Link[i] = =-1){ -memset (Used,false,sizeof(used)); -Ans + =DFS (i); -         } Aprintf"%d\n", ans); +     } the     return 0; -}
View Code

Hihocoder #1122: Two-figure binary map the best matching algorithm of Hungary

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.