//hdu 2444 The Accomodation of Students(是否二分图,找二分最大匹配数)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=250;
struct node
{
int a[N];
int len;
node():len(-1){}
}nd[N];
int path[N];
bool vis[N];
int n,m;
int color[N];//0 未染色,1 白色 2 黑色
int flag;
void init()
{
node dd;
for(int i=0;i<=n;i++) nd[i]=dd;
flag=true;
memset(color,0,sizeof(color));
memset(path,0,sizeof(path));
}
void DFS(int x,int d)//判断是否是二分图
{
color[x]=d;
for(int i=0;i<nd[x].len;i++)
{
if(color[nd[x].a[i]]==0)
{
DFS(nd[x].a[i],d%2+1);
}
else if(color[nd[x].a[i]]==d)
{
flag=false;
return ;
}
}
}
bool findgroup(int x)
{
for(int i=0;i<nd[x].len;i++)
{
if(!vis[nd[x].a[i]])
{
vis[nd[x].a[i]]=true;
if(!path[nd[x].a[i]]||findgroup(path[nd[x].a[i]]))
{
path[nd[x].a[i]]=x;
return true;
}
}
}
return false;
}
int main()
{
n=0,m=0;
while(scanf("%d%d",&n,&m)==2)
{
init();
int b1,b2;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&b1,&b2);
if(nd[b1].len==-1) nd[b1].len=0;
nd[b1].a[nd[b1].len++]=b2;
if(nd[b2].len==-1) nd[b2].len=0;
nd[b2].a[nd[b2].len++]=b1;
}
DFS(1,1);
if(!flag)
{
printf("No\n");
}
else
{
int sum=0;
for(int i=1;i<=n;i++)
{
memset(vis,false,sizeof(vis));
if(findgroup(i)) sum++;
}
printf("%d\n",sum/2);
}
}
return 0;
}
hdu 2444 The Accomodation of Students(是否二分图,找二分最大匹配数)
最新推荐文章于 2019-09-01 19:42:46 发布