The model of the Bipartite Graph is an adaptation of a classic question. As shown in a graph theory book, there are n people and M positions, and each position can only be provided to one person, while each person can only be qualified for a limited position due to limited ability, ask if there is a way to make everyone have a job, and if not, how many people can provide a job at most. If you have read this classic question, the idea of this question is as follows: Consider n questions as N points belonging to X parts of the Bipartite Graph, think of M tips as M Points belonging to y parts. Use the Hungary algorithm to match the two parts. When a point cannot match, the algorithm ends.
Because of the characteristics of the two-part graph, each vertex in Part X has only two edges connected to part y, using this feature, we can greatly optimize this algorithm (the program below is a brainless Hungary, and it is useless .... )
# Include <iostream>
# Include <cstdio>
# Include <math. h>
# Include <string. h>
Using namespace STD;
Intmatch [1009], visit [1009] = {0}, N, map [1009] [1009] = {0 };
Int DFS (int K)
{
For (INT I = 0; I <= n-1; I ++)
{
If (Map [k] [I] = 1 & visit [I] = 0)
{
Visit [I] = 1;
If (Match [I] =-1) | (DFS (Match [I]) = 1 ))
{
Match [I] = K;
Return 1;
}
}
}
Return 0;
}
Int main ()
{
Int M, ANS = 0, x, y;
Scanf ("% d", & N, & M );
For (INT I = 1; I <= m; I ++)
{
Scanf ("% d", & X, & Y );
Map [I] [x] = 1;
Map [I] [Y] = 1;
}
Memset (match, 255, sizeof (MATCH ));
For (INTI = 1; I <= m; I ++)
{
Memset (visit, 0, sizeof (visit ));
If (DFS (I) = 1) ans ++; else break;
}
Printf ("% d", ANS );
Scanf ("% d", & N, & M );
Return0;
}
Bzoj 1191: [hnoi2006] Superhero hero [bipartite graph matching]