题意:
一些奶牛只有在特定的围栏中才能产奶,要求合理安排使能产奶的奶牛数达到最大。
要点:
二分图裸题,最近刚学了二分图,看下面的参考博客,写的比较好:
参考博客:匈牙利算法
15479500 | Seasonal | 1274 | Accepted | 520K | 16MS | C++ | 736B | 2016-05-07 20:26:59 |
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int map[300][300], used[300],girl[300];
int n, m;bool find(int x)
{int i;for (i = 1; i <= m; i++){if (map[x][i] && !used[i]){used[i] = 1;if (girl[i] == -1 || find(girl[i]))//女孩还没人追或者追的人可以挪动{girl[i] = x;return true;}}}return false;
}int main()
{int i, num,temp;while (scanf("%d%d", &n, &m) != EOF){memset(map, 0, sizeof(map));memset(girl, -1, sizeof(girl));for (i = 1; i <= n; i++){scanf("%d", &num);while (num--){scanf("%d", &temp);map[i][temp] = 1;}}int num = 0;for (i = 1; i <= n; i++){memset(used, 0, sizeof(used));//每次都要重新赋值为0if (find(i))num++;}printf("%d\n", num);}return 0;
}