题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1403
代码:
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int n, m;
int p[1010][1010];
int book[1010];
int match[1010];
vector<int> g[1010];
int dfs(int u)
{
int i;
for (i = 0; i < g[u].size(); i++)
{
int v = g[u][i];
if (book[v] == 0 && p[u][v] == 1)
{
book[v] = 1;
if (match[v] == 0 || dfs(match[v]))
{
match[v] = u;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
int cases = 1;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
for (int i = 0; i <= n; i++) g[i].clear();
int ans = 0;
memset(match, 0, sizeof(match));
memset(p, 0, sizeof(p));
int a, b;
while (m--)
{
scanf("%d%d", &a, &b);
p[a][b] = 1;
g[a].push_back(b);
}
for (int i = 1; i <= n; i++)
{
memset(book, 0, sizeof(book));
if (dfs(i))
ans++;
}
printf("Case %d: %d\n", cases++, n - ans);
}
return 0;
}
版权声明:转载请注明出处。
原文地址:http://blog.csdn.net/u014427196/article/details/48020945