The main idea: give a binary graph, to find its maximum match.
Hungarian algorithm board question.
Code: (Poorly written)
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intn,m,hed[ the],cnt;structeg{intTO,NXT;} e[10005];voidAeintFintt) {e[++cnt].to =T; E[CNT].NXT=Hed[f]; HED[F]=CNT;}intfa[ the],vis[ the];BOOLDfsintu) {Vis[u]=1; for(intj=hed[u];j;j=e[j].nxt) { intto =e[j].to; if(Vis[to])Continue; Vis[to]=1; if(!fa[to]| |DFS (Fa[to])) {Fa[to]=u; return 1; } } return 0;}structass{intx, y;} as[102];inttot;BOOLCMP (A,ass b) {returna.x<b.x;}intMain () {scanf ("%d%d",&n,&m); intf,t; while(1) {scanf ("%d%d",&f,&t); if(f==-1&&t==-1) Break; AE (f,t); } intAns =0; for(intI=1; i<=n;i++) {memset (Vis,0,sizeof(VIS)); if(Dfs (i)) ans++; } printf ("%d\n", ans); for(inti=n+1; i<=m;i++) { if(Fa[i]) { as[++tot].x=Fa[i]; as[tot].y=i; }} sort ( as+1, as+1+tot,cmp); for(intI=1; i<=tot;i++) printf ("%d%d\n", as[I].x, as[I].Y]; return 0;}
Problem of pilot pairing scheme