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"