关闭

ACdream-1030-Matching

112人阅读 评论(0) 收藏 举报
分类:

Description

Compute the maximum cardinality matching of given undirected graph G

.

Input

The first line contains one integer n

, which denotes the number of nodes.

The following n

lines denotes the adjacency matrix A of graph G

.

(1n100

, Ai,j{0,1},Ai,i=0,Ai,j=Aj,i

)

Output

The only integer equals to the size of maximum cardinality matching.

Sample Input

3
0 1 1
1 0 1
1 1 0

Sample Output

1

Hint

One of possible matchings is to pair node 1

and 2, leaving node 3 alone.



二分图的基数最大匹配,匈牙利算法的模板题吧


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=105;
int g[maxn][maxn],match[maxn*maxn];
bool vis[maxn];
int n;

bool dfs(int u)
{
    vis[u]=true;
    for(int i=1;i<=n;i++)
    {
        if(g[u][i]==0||vis[i]) continue;
        vis[i]=true;
        if(match[i]==-1||dfs(match[i]))
        {
            match[i]=u;
            match[u]=i;
            return true;
        }
    }
    return false;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            scanf("%d",&g[i][j]);
        int ans=0;
        memset(match,-1,sizeof(match));
        for(int i=1;i<=n;i++)
        {
            if(match[i]==-1)
            {
                memset(vis,false,sizeof(vis));
                if(dfs(i))
                {
                    ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:61106次
    • 积分:4131
    • 等级:
    • 排名:第7752名
    • 原创:370篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    最新评论