hdu2444 判断二分图+最大匹配

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define maxn 210
int map[maxn][maxn],color[maxn];
int vis[maxn],match[maxn],n;
int bfs(int u,int n)
{
    int i;
    queue<int>q;
    q.push(u);
    color[u]=1;
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        for(i=1;i<=n;i++)
        {
            if(color[i]==-1&&map[v][i])
            {
                q.push(i);
                color[i]=!color[v];
            }
            if(color[i]==color[v]&&map[v][i])
                return 0;
        }
    }
    return 1;
}
int dfs(int u)
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        if(map[u][i]&&!vis[i])
        {
            vis[i]=1;
            if(match[i]==-1||dfs(match[i]))
            {
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(color,-1,sizeof(color));
        memset(map,0,sizeof(map));
        memset(match,-1,sizeof(match));
        for(i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            map[x][y]=map[y][x]=1;
        }
        int flag=1;
        for(i=1;i<=n;i++)
        {
            if(color[i]==-1&&!bfs(i,n))
            {
                flag=0;
                break;
            }
        }
        if(!flag)
        {
            printf("No
");
            continue;
        }
        int ans=0;
        for(i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                ans++;
        }
        printf("%d
",ans/2);
    }
}
原文地址:https://www.cnblogs.com/sweat123/p/4675843.html