hdu 2444 The Accomodation of Students( 判断二分图+最大匹配)

判断一个图是不是二分图,是的话求二分匹配最大匹配数。
    主要是怎么判断一个图是不是二分图。不妨选取某个点作为起点并 染为某种颜色、同时把与它相邻的元素染为对立的颜色,进行BFS,如果 到那步发现当前点和相邻点的颜色一样,那么就出现了矛盾,就不是二
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <queue>
#include <map>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0xFFFFFFFF
#define pi acos(-1.0)
#define eps 1e-4
#define maxn 100010
#define MOD 1000000007

int n,m,ans;
int color[300];
int match[300],visit[300];

struct Eage
{
    int from,to,next;
} edge[90000];
int tot,head[300];
int Color()
{
    queue<int> q;
    memset(color,-1,sizeof(color));
    color[1] = 1;
    q.push(1);
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        for(int i = head[x]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if(color[v] == -1)
            {
                color[v] = 1-color[x];
                q.push(v);
            }
            else if(color[v] == color[x])
                return 1;
        }
    }
    return 0;
}
int DFS(int x)
{
    for(int i = head[x]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(visit[v])
            continue;
        visit[v] = 1;
        if(match[v] == -1 || DFS(match[v]))
        {
            match[v] = x;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int t,C = 1;
    //scanf("%d",&t);
    while(scanf("%d%d",&n,&m) != EOF)
    {
        tot = 0;
        int a,b;
        memset(head,-1,sizeof(head));
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d",&a,&b);
            edge[tot].from = a;
            edge[tot].to = b;
            edge[tot].next = head[a];
            head[a] = tot++;
            edge[tot].from = b;
            edge[tot].to = a;
            edge[tot].next = head[b];
            head[b] = tot++;
        }
        if(Color())
        {
            printf("No\n");
            continue;
        }
        ans = 0;
        memset(match,-1,sizeof(match));
        for(int i = 1; i <= n; i++)
        {
            memset(visit,0,sizeof(visit));
            ans += DFS(i);
        }
        printf("%d\n",ans/2);
    }
    return 0;
}

分图。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值