1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <iostream> #include <cstring> #include <algorithm>
using namespace std; const int maxn = 510; int n,m; int g[maxn][maxn],match[maxn]; bool vis[maxn];
int dfs(int u) { for (int i=1; i<=n; ++i) if (g[u][i] && !vis[i]) { vis[i] = 1; if (match[i]==-1 | dfs(match[i])) { match[i] = u; return 1; } } return 0; }
int solve() { for (int k=1; k<=n; ++k) for (int i=1; i<=n; ++i) if (g[i][k]) { for (int j=1; j<=n; ++j) if (g[i][k]+g[k][j] == 2) g[i][j] = 1; } int res = 0; memset(match,-1,sizeof match); for (int i=1; i<=n; ++i) { memset(vis,0,sizeof vis); if (dfs(i)) ++res; } return res; }
int main() { freopen("1.in","r",stdin); while (scanf("%d%d",&n,&m) && (n+m)) { memset(g,0,sizeof g); while(m--) { int x,y; scanf("%d%d",&x,&y); g[x][y] = 1; } printf("%d\n",n-solve()); } return 0; }
|