给定一个二分图G,M为G边集的一个子集,如果M满足当中的任意两条边都不依附于同一个顶点,则称M是一个匹配。

 

Reference:

google上搜"ByVoid 二分图"(被墙了T^T)

http://ycool.com/post/cfnym64

http://segmentfault.com/q/1010000000094642

http://www.cnblogs.com/kuangbin/archive/2012/08/26/2657446.html

 http://note.dypanda.com/post/2012-08-07/maximal-matching-problem

 

计算二分图的最大匹配:匈牙利算法

二分图的最大匹配、带权最大匹配

模板:

#include <stdio.h>
#include <string.h>
#define MAX 102

long n,n1,match;
long adjl[MAX][MAX];
long mat[MAX];
bool used[MAX];

FILE *fi,*fo;

void readfile()
{
    fi=fopen("flyer.in","r");
    fo=fopen("flyer.out","w");
    fscanf(fi,"%ld%ld",&n,&n1);
    long a,b;
    while (fscanf(fi,"%ld%ld",&a,&b)!=EOF)
        adjl[a][ ++adjl[a][0] ]=b;
    match=0;
}

bool crosspath(long k)
{
    for (long i=1;i<=adjl[k][0];i++)
    {
        long j=adjl[k][i];
        if (!used[j])
        {
            used[j]=true;
            if (mat[j]==0 || crosspath(mat[j]))
            {
                mat[j]=k;
                return true;
            }
        }
    }
    return false;
}

void hungary()
{
    for (long i=1;i<=n1;i++)
    {
        if (crosspath(i))
            match++;
        memset(used,0,sizeof(used));
    }
}

void print()
{
    fprintf(fo,"%ld",match);
    fclose(fi);
    fclose(fo);
}

int main()
{
    readfile();
    hungary();
    print();
    return 0;
}
View Code

相关文章:

猜你喜欢
  • 2021-07-19
  • 2021-06-01
  • 2021-10-10
  • 2021-11-13
  • 2022-12-23
  • 2022-12-23
  • 2021-02-06
相关资源
相似解决方案