匈牙利算法:http://blog.csdn.net/dark_scope/article/details/8880547
#include <stdio.h>
#include <string.h>
#define maxn 105
int g[maxn][maxn], use[maxn], n, m, k, res;
bool vis[maxn];
int dfs(int x)
{
for(int i = 0;i < m;i++)
{
if(g[x][i] == 1&&!vis[i])
{
vis[i] = true;
if(use[i] == -1||dfs(use[i]))//未匹配或者可以腾出
{
use[i] = x;
return true;
}
}
}
return false;
}
void hungary()
{
memset(use, -1, sizeof(use));
for(int i = 0;i < n;i++)
{
memset(vis, 0, sizeof(vis));
if(dfs(i)) res++;
}
}
int main()
{
int i, a, b;
while(scanf("%d", &n)&&n != 0)
{
scanf("%d %d", &m, &k);
memset(g, 0, sizeof(g));
for(i = 0;i < k;i++)
{
scanf("%d", &a);//没作用
scanf("%d %d", &a, &b);
if(a&&b) g[a][b] = 1;
}
res = 0;
hungary();
printf("%d\n", res);
}
}