#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
#define N 205
bool vis[N];
int match[N],n,color[N];
vector<int>q[N];
bool Color(int s)
{
int i;
for(i=0; i<q[s].size(); i++)
{
int v=q[s][i];
if(color[v]==color[s]) return 0;
if(color[v]==-1)
{
color[v]=!color[s];
if(!Color(v))
return 0;
}
}
return 1;
}
bool dfs(int u)
{
int i;
for(i=0; i<q[u].size(); i++)
{
int v=q[u][i];
if(!vis[v])
{
vis[v]=1;
if(match[v]==-1||dfs(match[v]))
{
match[v]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int m,i;
while(~scanf("%d%d",&n,&m))
{
for(i=1; i<=n; i++)
q[i].clear();
int a,b;
memset(color,-1,sizeof(color));
while(m--)
{
scanf("%d%d",&a,&b);
q[a].push_back(b);
q[b].push_back(a);
}
color[1]=1;
if(!Color(1))//判断是否符合二着色问题
puts("No");
else
{
//匈牙利算法求最大匹配
int ans=0;
memset(match,-1,sizeof(match));
for(i=1; i<=n; i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i)) ans++;
}
printf("%d\n",ans>>1);//无向图结果/2
}
}
return 0;
}
【hdu2444】判断二分图+求最大匹配
最新推荐文章于 2020-07-17 13:25:40 发布
本文介绍了一个使用C++实现的程序,该程序首先验证给定图是否为二分图,并对其进行二色性着色。接着利用匈牙利算法找出图的最大匹配数。代码中包含了输入边信息、验证二色性和执行匈牙利算法的具体实现。
8万+

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



