hdu 1150 二分图 最小点覆盖=最大匹配

到了算法会写,但看不懂题目要用啥算法的阶段……真是悲哀

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define M 100

int n, m, k;

typedef struct _Node{
    int to;
    struct _Node* next;
}Node, *pNode;
pNode map[M];

int match[M];
int visited[M];

int DFS(int k){
    pNode p = map[k];
    int t;
    while(p){
        if(visited[p->to]){
            p = p->next;
            continue;
        }
        else
            visited[p->to] = 1;
        t = match[p->to];
        match[p->to] = k;
        if(t==-1 || DFS(t))
            return 1;
        match[p->to] = t;
        p = p->next;
    }
    return 0;
}

int cal(){
    int i, sum = 0;
    memset(match, -1, sizeof(match));
    for(i=0; i<n; i++){
        if(!map[i]) continue;
        memset(visited, 0, sizeof(visited));
        if(DFS(i))
            sum++;
    }
    return sum;
}

int main() {
    int i, t, u, v;
    pNode p;
    freopen("in", "r", stdin);
    while(scanf("%d", &n), n){
        scanf("%d %d", &m, &k);
        memset(map, 0, sizeof(map));
        for(i=0; i<k; i++){
            scanf("%d %d %d", &t, &u, &v);
            if(!u || !v) continue;
            p = (pNode)malloc(sizeof(Node));
            p->to = v;
            p->next = map[u];
            map[u] = p;
        }
        printf("%d\n", cal());
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值