1 /*2 Hungarian Algorithm Template questions3 adjacency table Implementation, adjacency matrix timeout4 Maximum matching problem5 */6#include <cstdio>7#include <vector>8#include <cstring>9 using namespacestd;Ten Const intMAXN =505; Onevector<int> V[MAXN];//x = v[i][j] means I can be matched with x A intVIS[MAXN],MATCH[MAXN];//Vis[i] means I have been accessed (matched) to prevent repeated access to a point in each DFS, x = match[i] means x now and I match - BOOLDfsintx) - { the for(inti =0; I < v[x].size (); ++i) - { - intt =V[x][i]; - if(!Vis[t]) + { -VIS[T] =1; + if(Match[t] = =-1||DFS (match[t])) A{Match[t] = x;return true;} at } - } - return false; - } - intHungaryintN) - { in intAns =0; - for(inti =1; I <= N; ++i) to { +memset (Vis,0,sizeofvis); - if(Dfs (i)) + +ans; the } * returnans; $ }Panax Notoginseng intMain () - { the intt,n,m,x,y; +scanf"%d",&t); A while(t--) the { +scanf"%d%d",&n,&m); - for(inti =1; I <= N; ++i) $ v[i].clear (); $memset (Match,-1,sizeofmatch); - while(m--) - { thescanf"%d%d",&x,&y); - v[x].push_back (y);Wuyi } theprintf"%d\n", Hungary (n)); - } Wu return 0; -}
The problem of Nanyang 239----Matchmaker "Hungarian algorithm"