Algorithm exercise series-Hungary algorithm based on the maximum matching of the hiho1122 Bipartite Graph
Address: http://hihocoder.com/problemset/problem/1122
The key to this question is two problems: 1 point using bfs to construct a Bipartite Graph 2: For the node in S in the bipartite graph, traverse to find the augmented path (the Hungarian algorithm is used to find the maximum matching of the Bipartite Graph). Each time a augmented path is found, one more match is found.
The Code is as follows:
/* Note two points for this question: 1. Use bfs to construct a bipartite graph. 2. traverse the nodes in S to find the augmented path (the Hungary algorithm calculates the maximum matching of the Bipartite Graph) each time you find an extension path, you will find another matching */# include
# Include
# Include
Using namespace std; # define MAXSIZE 10010 struct GNode {int vertex; GNode * adj;}; class Graph {public: Graph (int v, int e) {// graph constructor initializes vertexNum = v; arcNum = e; memset (result, 0, sizeof (result); for (int I = 1; I <= vertexNum; I ++) {color [I] = 2; edges [I] = NULL; // The local variable must be initialized; otherwise, it cannot be compared with NULL} void initEdges (int u, int v); void showGraph (); bool findPath (int u); // traverses the extended int visited [MAXSIZE]; // identifies whether int color has been accessed [MAXSIZE]; // 0 1 indicates the relative color. 2 indicates the access to construct a bipartite graph ~ Graph () {for (int I = 1; I <= vertexNum; I ++) destroy (edges [I]);} bool bfs_travel (); // construct a bipartite graph by traversing the breadth: int vertexNum; int arcNum; GNode * edges [MAXSIZE]; int result [MAXSIZE]; // Save the vertex void destroy (GNode * pNode) that V2 matches in V1; // destroy the figure void initUndirectedEdges (int u, int v ); bool bfs (int v) ;}; void Graph: initEdges (int u, int v) {initUndirectedEdges (u, v); initUndirectedEdges (v, u);} void Graph:: initUndirectedEdges (int U, int v) {GNode * adjNode = new GNode (); adjNode-> vertex = v; adjNode-> adj = NULL; if (edges [u] = NULL) edges [u] = adjNode; else {GNode * pNode = edges [u]; while (pNode-> adj! = NULL) pNode = pNode-> adj; pNode-> adj = adjNode;} bool Graph: bfs (int v) {queue
Q; q. push (v); color [v] = 0; while (! Q. empty () {int u = q. front (); q. pop (); GNode * pNode = edges [u]; while (pNode! = NULL) {if (color [pNode-> vertex] = color [u]) return false; if (color [pNode-> vertex] = 2) {q. push (pNode-> vertex); color [pNode-> vertex] =! Color [u];} pNode = pNode-> adj;} return true;} bool Graph: bfs_travel () {for (int I = 1; I <= vertexNum; I ++) {if (color [I] = 2) if (! Bfs (I) return false;} return true;} void Graph: destroy (GNode * pNode) {if (pNode! = NULL) {destroy (pNode-> adj); delete pNode ;}}// find an extended bool Graph: findPath (int u) {GNode * pNode = edges [u]; while (pNode! = NULL) {int v = pNode-> vertex; if (! Visited [v]) {visited [v] = 1; // = 0: if (result [v] = 0 | findPath (result [v]) {result [v] = u; return true ;}} pNode = pNode-> adj;} return false;} void Graph: showGraph () {for (int I = 1; I <= vertexNum; I ++) {cout <I <"->"; GNode * p = edges [I]; while (p! = NULL) {cout <"(" <p-> vertex <")"; p = p-> adj;} cout <endl ;}} int main () {int N, M; cin> N> M; // input vertex and edge Graph g (N, M); for (int I = 0; I <M; I ++) {int u, v; cin> u> v; g. initEdges (u, v); // The structure graph is stored in an adjacent table} // g. showGraph (); int ans = 0; if (g. bfs_travel () {// determines whether a bipartite graph and an ID bipartite graph color [I] = 0 or 1for (int I = 1; I <= N; I ++) {if (! G. color [I]) {memset (g. visited, 0, sizeof (g. visited); if (g. findPath (I) ans ++ ;}}} cout <ans <endl; return 0 ;}/ * 5 43 21 35 41 5 */
Question:
The first thing to implement an algorithm is (1) Understanding the algorithm implementation process (through examples) (2) preliminarily determining the data structure required by the algorithm
(3) coding implementation
For a large problem or project, you need to think about the difficult problems that need to be solved immediately (for example, there are two points in this question), and then find algorithms to solve them one by one.