HK 算法 模板
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
int dist[N<<1],mx[N],my[N],m,n;
vector<int> map[N];
int que[N<<1],head,tail;
int bfs()
{
int i;
head=0;tail=-1;
for(i=1;i<=n;i++)
if(mx[i]==-1) que[++tail]=i;
for(i=0;i<=m+n;i++) dist[i]=0;
int flag=0;
while(head<=tail)
{
int u=que[head++];
for(i=0;i<map[u].size();i++)
{
int v=map[u][i];
if(dist[n+v]==0)
{
dist[n+v]=dist[u]+1;
if(my[v]!=-1)
{
dist[my[v]]=dist[n+v]+1;
que[++tail]=my[v];
}
else flag=1;
}
}
}
return flag;
}
int dfs(int u)
{
for(int i=0;i<map[u].size();i++)
{
int v=map[u][i];
if(dist[u]+1==dist[v+n])
{
int t=my[v];
dist[v+n]=0;
if(t==-1||dfs(t))
{
my[v]=u;
mx[u]=v;
return 1;
}
}
}
return 0;
}
int H_K()
{
int i;
for(i=0;i<=n;i++) mx[i]=-1;
for(i=0;i<=m;i++) my[i]=-1;
int ans=0;
while(bfs())
{
for(i=1;i<=n;i++)
if(mx[i]==-1&&dfs(i)) ans++;
}
return ans;
}