匈牙利模板
作者:
CCCkk
,
2022-07-16 09:35:00
,
所有人可见
,
阅读 176
邻接矩阵版
int n;
int d[N][N], match[N];
bool st[N];
bool find(int x)
{
for (int j = 1; j <= n; j ++ )
{
if (d[x][j] && !st[j])
{
st[j] = true;
if (match[j] == 0 || find(match[j]))
{
match[j] = x;
return true;
}
}
}
return false;
}
...
int cnt = 0;
for (int i = 1; i <= n; i ++ )
{
memset(st, false, sizeof st);
if (find(i)) cnt ++;
}
邻接表版
int n;
int h[N], e[M], ne[M], idx;
int match[N];
bool st[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;
}
...
for (int i = 1; i <= n; i ++ )
{
memset(st, false, sizeof st);
if (find(i)) cnt ++;
}