4 4
1 2
3 4
3 2
4 2

2

# HINT

$N \le 100,M \le 1000$

$A$能到$B$，那么在二分图中$A_{1}$$B_{2}$连接一条边（连边我们可以用floyed）。最后的答案即为总点数-最大匹配数。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

#define maxn (110)
#define maxm (1010)
int n,m,cho[maxn]; bool f[maxn][maxn],used[maxn];

inline int find(int x)
{
for (int i = 1;i <= n;++i)
if (f[x][i]&&!used[i])
{
used[i] = true;
if (!cho[i]||find(cho[i])) { cho[i] = x; return true; }
}
return false;
}

inline int hungry()
{
int ret = 0;
for (int i = 1;i <= n;++i)
{
memset(used,false,sizeof(used));
if (find(i)) ret++;
}
return ret;
}

int main()
{
freopen("1143.in","r",stdin);
freopen("1143.out","w",stdout);
scanf("%d %d",&n,&m);
while (m--) { int a,b; scanf("%d %d",&a,&b); f[a][b] = true; }
for (int k = 1;k <= n;++k)
for (int i = 1;i <= n;++i) if (f[i][k])
for (int j = 1;j <= n;++j)
f[i][j] |= (f[i][k]&f[k][j]);
printf("%d",n-hungry());
fclose(stdin); fclose(stdout);
return 0;
}


posted @ 2015-03-06 14:54  lmxyy  阅读(200)  评论(5编辑  收藏  举报