zoukankan      html  css  js  c++  java
  • 匈牙利算法求二分图求最大匹配

      无向图匈牙利算法求地的值除以2才是答案,因为每个边都匹配了两次,有向图算法得出的就是答案。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int maps[50][50],match[50],vis[50],n;
    bool Find(int u)
    {
        cout<<"the fa = "<<u<<endl;
        for(int i = 1; i <= n; i++)
        {
            if(maps[u][i])
            {
                if(!vis[i])
                {
                    vis[i] = 1;
                    if(match[i] == -1 || Find(match[i]))
                    {
                        cout<<"the son = "<<i<<endl;
                        match[i] = u;
                        return true;
                    }
                    else cout<<"don't find his son"<<endl;
                }
            }
        }
        return false;
    }
    int slove()
    {
        memset(match,-1,sizeof(match));
        int ans = 0;
        for(int i = 1; i <= n; i++)
        {
            memset(vis,0,sizeof(vis));
            if(Find(i))
            ans++;
        }
        return ans;
    }
    int main()
    {
        int m,a,b,t;
        ///freopen("Ain.txt","r",stdin);
        cin>>t;
        while(t--)
        {
            memset(maps,0,sizeof(maps));
            cin>>n>>m;
            while(m--)
            {
                cin>>a>>b;
                maps[b][a] = 1;
                maps[a][b] = 1;
            }
            int ans = slove();
            cout<<"ans = "<<ans/2<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Linux如何修改命令提示符
    Linux命令详解-install
    Linux命令详解-info
    Linux命令详解-man
    Linux命令详解-printf
    Linux命令详解-echo
    Linux命令详解-whatis
    Linux命令详解-file
    Linux命令详解-help
    Linux命令详解-type
  • 原文地址:https://www.cnblogs.com/jifahu/p/5520964.html
Copyright © 2011-2022 走看看