AcWing 373. 車的放置
原题链接
简单
作者:
一天不A浑身难受
,
2021-03-20 13:25:51
,
所有人可见
,
阅读 363
#include <iostream>
#include <cstring>
using namespace std;
const int N = 555;
int n, m, t;
bool st[N];
int h[N], e[N], ne[N], idx;
int g[N][N];
int match[N];
void add(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
bool find(int x)
{
for (int i = h[x]; i != -1; i = ne[i])
{
int j = e[i];
if (!st[j])
{
st[j] = true;
if (match[j] == 0 || find(match[j]))
{
match[j] = x;
return true;
}
}
}
return false;
}
int main()
{
cin >> n >> m >> t;
memset(h, -1, sizeof h);
while (t--)
{
int a, b;
cin >> a >> b;
g[a][b] = -1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (g[i][j] != -1)
add(i, n + j); //为了防止行号列号重复, 把行号设置为1~n,列号设置为n+1~n+m
int res = 0;
for (int i = 1; i <= n; i++)
{
memset(st, false, sizeof st);
if (find(i))res++;
}
cout << res;
}