Topic Link: http://hihocoder.com/problemset/problem/1122, Hungarian algorithm naked question.
Just learn the two points to match, or to more brush questions.
This problem can be directly set template, I am based on the topic above to do, so first added a dyeing optimization, the effect of the general bar.
#include <iostream>#include<cstdio>#include<vector>#include<queue>#include<cmath>#include<string>#include<string.h>#include<algorithm>using namespacestd;#defineLL __int64#defineEPS 1e-8#defineINF 1e8#defineLson L, M, RT << 1#defineRson m + 1, R, RT << 1 | 1Const intMOD =2333333; Const intMAXN = ++5; Vector<int>E[MAXN];intCOLOR[MAXN], MATCH[MAXN], VIS[MAXN];voidDFS (ints) { for(inti =0; I < e[s].size (); i++) { intj =E[s][i]; if(!Color[j]) {Color[j]=3-Color[s]; DFS (j); } }}BOOLFindintv) { if(Vis[v])return false; VIS[V]=1; for(inti =0; I < e[v].size (); i++) { intU =E[v][i]; if(!Vis[u]) {Vis[u]=1; if(!match[u] | |find (Match[u])) {Match[u]=v; return true; } } } return false;}intMain () {intN, M, U, V, res =0; scanf ("%d%d", &n, &m); memset (color,0,sizeof(color)); memset (Match,0,sizeof(match)); while(m--) {scanf ("%d%d", &u, &v); E[u].push_back (v); E[v].push_back (U); } for(inti =1; I <= N; i++) { if(!Color[i]) {Color[i]=1; DFS (i); } } for(inti =1; I <= N; i++) { if(Color[i] = =1) {memset (Vis,0,sizeof(VIS)); Res+=find (i); }} printf ("%d\n", res);}
Hungarian algorithm for maximum matching of hihocoder 11,222 fractal graph