Binary graph matching--Hungarian algorithm

Source: Internet
Author: User

Daily Spit groove: hahaha haha ha ha ha ha ha haha today is the only time so long since the only big cool (? ) of the day. I, Zhao wit small dodger son with 10 minutes ac the xxx template! Applause and encouragement!

The idea still see XXX blog 70175814 anyway little Dodger son just to stick code

Examples

Rokua P3386 "template" binary map matching

Topic background

Two-part diagram

Title Description

Given a binary graph, the number of nodes is n,m, the number of edges is e, and the maximum matching number of two graphs is obtained.

Input/output format

Input format:

First line, N,m,e

Second to e+1 lines, two positive integer u,v per line, indicating that U,v has a connecting edge

Output format:

Total row, binary graph max match

Input/Output sample

Input Sample # #:
1 1 11 1
Sample # # of output:
1

Description

N, M is less than or equal to 1000,u and V are positive integers less than n and M respectively.

v>m  case. Please put  v>m< Span class= "Strut bottom" >   the data consciously filtered out.

Code

#include <cstdio>#include<iostream>#include<cstring>using namespacestd;intN, M, p, tot =0, ans =0;inta[1001], b[1001], t[1001][1001], vis[1001];intDfsintu) {     for(intv =1; V <= m; v++)        if(T[u][v] &&!Vis[v]) {Vis[v]=1; if(!b[v] | |DFS (B[v])) {A[u]=v; B[V]=u; return 1; }        }    return 0;}voidMaxmatch () { for(inti =1; I <= N; i++)        if(!A[i]) {memset (Vis,0,sizeofvis); Ans+=DFS (i); } printf ("%d\n", ans);}intMain () {memset (A,0,sizeofa); memset (b,0,sizeofb); memset (T,0,sizeoft); scanf ("%d%d%d", &n, &m, &p);  for(inti =1; I <= p; i++)    {        intx, y; scanf ("%d%d", &x, &y); if(X > N | | y > m)Continue; Tot++; T[x][y]=1;    } maxmatch (); return 0;}

Binary graph matching--Hungarian algorithm

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.