POJ 2594 Treasure Exploration
\(POJ\) \(2594\) \(Treasure\) \(Exploration\)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510;
int n, m;
int g[N][N];
// 匈牙利
int match[N], st[N];
int dfs(int u) {
for (int i = 1; i <= n; i++)
if (g[u][i] && !st[i]) {
st[i] = 1;
if (match[i] == -1 | dfs(match[i])) {
match[i] = u;
return 1;
}
}
return 0;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("POJ2594.in", "r", stdin);
#endif
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;
}
// Floyd传递闭包
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
g[i][j] |= g[i][k] & g[k][j];
int res = 0;
memset(match, -1, sizeof match);
for (int i = 1; i <= n; i++) {
memset(st, 0, sizeof st);
if (dfs(i)) res++;
}
printf("%d\n", n - res);
}
return 0;
}