跑的慢的很。
#include<cstdio>
#define N 4009
#define M 200009
struct node
{
int v, w, nxt;
}e[M];
int head[N], cnt = 1;
inline void add(int u, int v, int w)
{
e[++ cnt].v = v, e[cnt].w = w, e[cnt].nxt = head[u], head[u] = cnt;
}
int dis[N], now[N];
#include<queue>
using std::queue;
#include<cstring>
using std::memset;
int s, t;
inline bool bfs()
{
memset(dis, -1, sizeof dis);
queue < int > q;
q.push(s);
dis[s] = 0;
register int u, v, i;
while (!q.empty())
{
u = q.front();
q.pop();
now[u] = head[u];
for (i = now[u];i;i = e[i].nxt)
{
v = e[i].v;
if (e[i].w > 0 && dis[v] == -1)
{
dis[v] = dis[u] + 1;
if (v == t)
return 1;
q.push(v);
}
}
}
return 0;
}
inline int min(int a, int b)
{
return a < b ? a : b;
}
int dfs(register int u, register int flow = 1e9)
{
if (u == t)
return flow;
register int v, w, i, k, lost = flow;
for (i = now[u];i && lost;i = e[i].nxt)
{
v = e[i].v, w = e[i].w;
now[u] = i;
if (w > 0 && dis[v] == dis[u] + 1)
{
k = dfs(v, min(lost, w));
if (!k)
{
dis[v] = -1;
continue;
}
lost -= k;
e[i].w -= k;
e[i ^ 1].w += k;
}
}
return flow - lost;
}
inline int read()
{
register int ret = 0;
register bool f = 1;
register char ch = getchar();
while (ch < '0' || ch > '9')
(ch == '-') ? f = 0 : 0, ch = getchar();
while (ch >= '0' && ch <= '9')
ret = (ret << 1) + (ret << 3) + (ch ^ 48), ch = getchar();
return f ? ret : -ret;
}
int main()
{
register int n, n1 = read(), n2 = read(), m = read(), i, u, v, ans = 0;
n = n1 + n2;
s = 0, t = n + 1;
for (i = 1;i <= m;++ i)
u = read(), v = read() + n1, add(u, v, 1), add(v, u, 0);
for (i = 1;i <= n1;++ i)
add(0, i, 1), add(i, 0, 0);
for (i = n1 + 1;i <= n1 + n2;++ i)
add(i, t, 1), add(t, i, 0);
while (bfs())
ans += dfs(s);
printf("%d", ans);
return 0;
}