zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    第三次上机练习
    第三次作业
    第二次上级练习
    第二次作业
    第一次上机练习
    第一次作业
    4.20
    4.16
    4.10
    4.9
  • 原文地址:https://www.cnblogs.com/sweat123/p/4675843.html
Copyright © 2011-2022 走看看