Highly recommended Hungarian algorithm introduction: http://www.renfei.org/blog/bipartite-matching.html
1#include"iostream"2#include"Vector"3#include"Memory.h"4 using namespacestd;5 #defineSwap (A,b,t) ((t) = (x), (x) = (y), (y) = (t))6 #defineMAXN 10107 intL[MAXN][MAXN];8 BOOLFLAG[MAXN];9 intRE[MAXN];Ten intK,m,n; One intans; A - intFindinta) - { the for(inti =0; I <= N; ++i) { - if(L[a][i] = =1&& Flag[i] = =0) { -Flag[i] =1; - if(Re[i] = =0||find (Re[i])) { +Re[i] =A; - return 1; + } A } at } - return 0; - } - - voidInit () - { inAns =0; - for(inti =0; I <= m; ++i) to for(intj =0; J <= N; ++j) +L[I][J] =0; -memset (Re,0,sizeof(re)); the } * $ intMain ()Panax Notoginseng { - the while(Cin >>k,k) { + init (); AAns =0; theCIN >> M >>N; + for(inti =0; I < K; ++i) { - intb; $Cin >> a >>b; $L[A][B] =1; - } - for(inti =1; I <= m; ++i) { thememset (Flag,0,sizeof(flag)); - if(Find (i)) ans + +;Wuyi } thecout << ans <<Endl; - } Wu return 0; -}
DFS
Owed a BFS
hdu2063 roller coaster (maximum two-point match)