'commit'
This commit is contained in:
52
TangDou/AcWing/ErFenTu/POJ2594.cpp
Normal file
52
TangDou/AcWing/ErFenTu/POJ2594.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user