HUD 2444 The Accomodation of Students (二分图染色+最大匹配)

时间:2021-09-23 21:35:14
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 2010
using namespace std;
int n,m,num,head[maxn],f[maxn],match[maxn],color[maxn];
struct node
{
int u,v,pre;
}e[maxn*maxn];
void Add(int from,int to)
{
num++;
e[num].u=from;
e[num].v=to;
e[num].pre=head[from];
head[from]=num;
}
bool Color(int s)
{
for(int i=head[s];i;i=e[i].pre)
if(!color[e[i].v])
{
color[e[i].v]=-*color[s];
if(!Color(e[i].v))return ;
}
else if(color[e[i].v]==color[s])return ;
return ;
}
int Dfs(int s)
{
for(int i=head[s];i;i=e[i].pre)
if(f[e[i].v]==)
{
f[e[i].v]=;
if(match[e[i].v]==||Dfs(match[e[i].v]))//XXXXX又是这句错了 下次再写错就XXXX
{
match[e[i].v]=s;
return ;
}
}
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
num=;
memset(f,,sizeof(f));
memset(head,,sizeof(head));
memset(color,,sizeof(color));
memset(match,,sizeof(match));
int u,v;
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
Add(u,v);Add(v,u);
}
if(n==)
{
printf("No\n");
continue;
}
color[]=;
if(!Color())
{
printf("No\n");
continue;
}
int ans=;
for(int i=;i<=n;i++)
{
memset(f,,sizeof(f));
ans+=Dfs(i);
}
printf("%d\n",ans/);
}
return ;
}