const int M = 505;
Graph<M,M*M> g;
class MaxNumMatch{
public:
void Clear(){
CLR(Match,-1); g.Clear();
}
int GetMaxNumMatch(int s,int t){
int MatchNum = 0;
for(int i = s;i <= t;++i){
CLR(Vist,false);
if(Dfs(i)) MatchNum++;
}
return MatchNum;
}
bool Dfs(int cur){
for(int i = g.H[cur];i != -1;i = g.Next[i])
{
if(!Vist[ g.Num[i] ]){
Vist[ g.Num[i] ] = true;
if(Match[ g.Num[i] ] == -1 || Dfs(Match[ g.Num[i] ]))
{
Match[ g.Num[i] ] = cur;
return true;
}
}
}
return false;
}
private:
int Match[M];
bool Vist[M];
};
需要加邻接表模板,头文件string.h,宏定义#define CLR(arr,v) memset(arr,v,sizeof(arr))
需要先调用Clear()函数。

本文介绍了一种求解最大匹配数问题的算法实现,通过邻接表存储图结构,并使用深度优先搜索(DFS)来寻找可能的最大匹配组合。文章详细展示了类`MaxNumMatch`的定义及其实现方法,包括初始化、清除状态、获取最大匹配数量等功能。
154

被折叠的 条评论
为什么被折叠?



