Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=1179
Main topic:
The title is too long, the simple meaning is: There are n wands, M wizards, wands have a number of matching wizards. But a magician.
Only one wand can be matched. So the question is: how many magicians can get the magic wand at most.
Ideas:
Make a dichotomy, one side is a wand, the other is a magician. The corresponding match as the side of the binary graph. Using the Hungarian algorithm, find out two
What is the maximum matching of the graph?
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int maxn = 110;bool map[maxn][maxn],mask[maxn];int n,m,cx[maxn],cy[maxn];int FindPath (int u) {for (int i = 1; I <= M; ++i) {if (Map[u][i] &&!mask[i]) {mask[i] = 1; if (cy[i] = =-1 | | Findpath (Cy[i])) {Cy[i] = u; Cx[u] = i; return 1; }}} return 0;} int Maxmatch () {int ret = 0; for (int i = 1; I <= N; ++i) cx[i] = 1; for (int i = 1; I <= M; ++i) cy[i] = 1; for (int i = 1; I <= N, ++i) {if (cx[i] = = 1) {for (int j = 1; j <= M; ++j) MASK[J] = 0; RET + = Findpath (i); }} return ret;} int main () {int d,w; while (~SCANF ("%d%d", &n,&m)) {memset (map,false,sizeof (MAP)); for (int i = 1; I <= M; ++i) { scanf ("%d", &d); while (d--) {scanf ("%d", &w); Map[w][i] = 1; }} printf ("%d\n", Maxmatch ()); } return 0;}
HPU1179 Ollivanders:makers of Fine wands since 382 BC. "Binary graph Max Match"