hdu 2444 The Accomodation of Students(是否二分图,找二分最大匹配数)

//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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u014068781

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值