ACM Learning-Hungary Matches

Source: Internet
Author: User

Hungarian match first said a simple situation, more is the same, such as A,b,c to find objects, a asked B there is no object, B said no, Ok,a told B after your object is me, if C also started in the surrounding (connected nodes) Find objects, if found B, found that B already has objects, Ask B is there anyone around you single, b find a single, tell a after you are the object of me, otherwise go to ask a around there is no one single, and so on and so on, once found like a such a single B must reluctantly admitted his object is C. Summed up is this, once a node to find the object, the node it visited is a long chain, the head is to find the object of the node, the tail is single person, if there is a middle node, the middle is already has the object of the person. After execution, this chain has no objects except the head, others have objects, that is, to find the object is to let others admit that his object is me, but I myself have no object.


ACM Learning-Hungary matches. CPP: The entry point that defines the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace Std;


const int v = 13;
int Edge[v][v] = {
A B C D E F G H I J K L M
{0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},//a
{1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1},//b
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//c
{0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},//d
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},//e
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//f
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},//g
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0},//h
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},//i
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},//j
{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0},//k
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1},//l
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0}//m
};
Char t[] = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M '};


const int n = 100;
Vector<int> G[v];
int from[v], tot;
BOOL Use[v];


BOOL Match (int x) {
for (int i = 0; i < g[x].size (); i++) {
if (!use[g[x][i]])
{
Use[g[x][i]] = true;
if (from[g[x][i]] = = 1 | | match (from[g[x][i]))
{
if (from[g[x][i]] = =-1)
{
cout << T[g[x][i]] << "hubby is:" << t[x << Endl;
}
else{
cout << T[g[x][i]] << "old husband:" << T[from[g[x][i]] << Endl;
cout << T[g[x][i]] << "New husband is:" << t[x] << Endl;
}

From[g[x][i]] = x;
return true;
}
}

}
return false;


}
int Hungary () {
tot = 0;
memset (from,255,sizeof from);
for (int i = 0; i < V; i++) {
memset (use,0,sizeof use);
if (Match (i)) tot++;
}
return tot;
}
int _tmain (int argc, _tchar* argv[])
{
for (int i = 0; i < V; i++) {
for (int j = 0; J < V; j + +) {
if (Edge[i][j])
G[i].push_back (j);

}
}
for (int i = 0; i < V; i++) {
for (int j = 0; J < G[i].size (); j + +) {

cout << T[i] << ":" << T[g[i][j]] << ends<<ends;
}
cout << Endl;
}
cout << "tot=" << Hungary () << Endl;
for (int i = 0; i < V; i++)
{
if (from[i] = =-1) {
cout << T[i] << "First and" << from[i << Endl;
}
else{
cout << T[i] << "First and" << T[from[i] << Endl;
}
//

}
return 0;
}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ACM Learning-Hungary Matches

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.