我有以下代码,它是 BPM 的实现(来自图论的二分匹配)
#include <iostream>
#include <cstring>
using namespace std;
#define M 128
#define N 128
bool graph[M][N];
bool seen[N];
int matchL[M],matchR[N];
int n=4;
int m=4;
bool bpm(int u){
for(int v=0;v<n;v++) if(graph[u][u])
{
if (seen[v]) continue;
seen[v]=true;
if(matchR[v] <0 || bpm(matchR[v])){
matchL[u]=v;
matchR[v]=u;
return true;
}
}
return false;
}
int main(){
graph[0][1]=1;
graph[0][3]=1;
graph[1][3]=1;
graph[0][2]=1;
memset(matchL,-1,sizeof(matchL));
memset(matchR,-1,sizeof(matchR));
int cnt=0;
// memset(seen,0,sizeof(seen));
for(int i=0;i<m;i++){
memset(seen,0,sizeof(seen));
if(bpm(i)) cnt++;
}
cout<<cnt<<endl;
return 0;
}
该代码的定义cnt和目的如下。
给定一个表示为 m×n 矩阵的二分图,其中当当且
graph[i][j]仅当从鸽子到洞true存在一条边时,计算可以找到洞的最大鸽子数量(每只鸽子一个)和最佳分配。ij
graph[m][n],matchL[n],matchR[m]和seen[m]是全局数组。main()初始化matchL[]并matchR[]在-1所有组件中。main()对所有鸽子进行循环,i并在每次迭代中- 清除
seen[]所有0组件 - calls
bpm(i)and increments themaxflowcounter bpm(i)returnstrueiff pigeonican be assigned a hole
- 清除
cntcontains the number of happy pigeons.
In my case, cnt's value is output as 0. Does this graph algorithm work correctly or have I made some error?