Rokua P2764 Minimum path coverage problem "Hungarian algorithm"

Source: Internet
Author: User

Classical binary graph matching problem. Each point is split into two, and for each edge (I,J) connection (I,j+n) in the original, the minimum path overlay is the maximum match of the points N-binary graph. The program follows the DSF directly along the match.

#include <iostream>#include <cstdio>using namespaceStdConst intn=505, m=120005;intN,m,h[n],cnt,lk[n],t,v[n],ans;structqwe{intNe,to;} E[M];intRead () {intR=0, f=1;CharP=getchar (); while(p>' 9 '|| p<' 0 ')    {if(p=='-') f=-1;    P=getchar (); } while(p>=' 0 '&&p<=' 9 ') {r=r*Ten+p-48;    P=getchar (); }returnR*f;}voidAddintUintV) {cnt++;    E[cnt].ne=h[u];    E[cnt].to=v; h[u]=cnt;}BOOLFINDD (intu) { for(intI=h[u];i;i=e[i].ne)if(v[e[i].to]!=t) {v[e[i].to]=t;if(!lk[e[i].to]| |                FINDD (Lk[e[i].to])) {lk[e[i].to]=u; lk[u]=e[i].to;return 1; }        }return 0;}voidPrin (intu) {u+=n; Doprintf"%d ", u=u-n); while(V[u]=t,u=lk[u]); Puts"");}intMain () {N=read (), M=read (); for(intI=1; i<=m;i++) {intX=read (), Y=read ();    Add (x,y+n); } for(intI=1; i<=n;i++)if(!lk[i]) {t++;if(Findd (i)) ans++; } t++; for(intI=1; i<=n;i++)if(v[i]!=t) Prin (i); printf"%d\n", N-ans);return 0;}

Rokua P2764 Minimum Path overlay problem "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.