题目链接:http://poj.org/problem?id=1274
题意:一个农场主,建了m个高科技产奶器,他由n头奶牛,每头奶牛值喜欢其中的几个产奶器,只有去自己喜欢的产奶器那里才能产奶,现问你最多几头牛能产奶
解析:二分图匹配的经典问题,直接上匈牙利
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 405;
struct node
{
int to,next;
}edge[5*maxn];
int head[maxn],cnt;
int match[maxn],vis[maxn];
void init()
{
memset(head,-1,sizeof(head));
memset(match,0,sizeof(match));
cnt = 0;
}
void addEdge(int u,int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
bool dfs(int u)
{
for(int i=head[u];i!=-1;i = edge[i].next)
{
int v = edge[i].to;
if(!vis[v])
{
vis[v] = 1;
if(match[v]==0||dfs(match[v]))
{
match[v] = u;
match[u] = v;
return true;
}
}
}
return false;
}
int main(void)
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=1;i<=n;i++)
{
int s,x;
scanf("%d",&s);
for(int j=0;j<s;j++)
{
scanf("%d",&x);
addEdge(i,x+n);
}
}
int ans = 0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
vis[i] = 1;
if(dfs(i))
ans++;
}
printf("%d\n",ans);
}
return 0;
}