The maximum match of the binary graph. Connect the two attributes of each weapon with this weapon. Enumeration property, which cannot be matched on the output.
Basic concepts: (oral unofficial)
Two-part diagram: The vertices of a graph are divided into two parts, each of which has no edges attached to each point.
Match: In the binary graph, find some edges, each side has no common point.
Maximum match: the maximum match.
Perfect match: Each point is used on the match, the number of hops = points.
In fact, the maximum matching of the binary graph can be explained by the maximum flow algorithm. Each edge has a capacity of 1. But the efficiency is not high.
Hungarian algorithm.
#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN =1000000+Ten;Const intMAXM =4000000+Ten;Const intMAXV =10000+Ten;intN,eid;intH[MAXV],TO[MAXM],NEXT[MAXM],V[MAXM];intP[MAXN];BOOLVIS[MAXV];voidAddintAintb) {V[eid]= b; Next[eid] = H[a]; H[a] = eid++;}BOOLFindintu) {if(Vis[u])return false; Vis[u]=true; for(inti = h[u];~i; i =Next[i]) { if(!p[v[i] | |find (P[v[i])) {P[v[i]]=u; return true; } } return false;}intMain () {scanf ("%d",&N); memset (H,-1,sizeof(h)); for(inti =1, A, B; I <= N; i++) {scanf ("%d%d",&a,&b); Add (a,i); Add (b,i); } intRes; for(res =1; Res <=10000; res++) {memset (Vis,0,sizeof(VIS)); if(!find (RES)) Break; } printf ("%d\n", res-1); return 0;}
bzoj1854: [Scoi2010] Games