hdu2063 过山车【二分图匹配】

82 篇文章 0 订阅
57 篇文章 0 订阅

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063
题意:中文题
解析:二分图匹配裸题

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000;
struct node
{
    int to,next;
}edge[maxn];
int head[maxn],cnt;
int match[maxn],vis[maxn];
void init()
{
    memset(head,-1,sizeof(head));
    memset(match,0,sizeof(match));
    cnt = 0;
}
void addEdge(int from,int to)
{
    edge[cnt].to = to;
    edge[cnt].next = head[from];
    head[from] = cnt++;
}
bool dfs(int u)
{
    for(int i=head[u];i!=-1;i = edge[i].next)
    {
        int v = edge[i].to;
        if(vis[v]) continue;
        vis[v] = 1;
        if(!match[v] || dfs(match[v]))
        {
            match[v] = u;
            match[u] = v;
            return true;
        }
    }
    return false;
}
int main(void)
{
    int k;
    while(~scanf("%d",&k)&&k)
    {
        int n,m;
        init();
        scanf("%d %d",&m,&n);
        for(int i=0;i<k;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            addEdge(x,y+m);
        }
        int ans = 0;
        for(int i=1;i<=m;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值