poj 1469

本文介绍了一种解决二分图最大匹配问题的匈牙利算法实现,通过使用邻接矩阵来表示图,并实现了匈牙利算法求解二分图的最大匹配数。文章提供了完整的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用浙大的模版A的第一道二分图最大匹配的模版题

//============================================================================
// Name        : 1469.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 310

int match1[MAXN], match2[MAXN];
int mat[MAXN][MAXN];
int m, n, temp, t, T, ans;

int hungary(){
    int s[MAXN], t[MAXN], p, q, ret = 0, i, j, k;
    memset(match1, -1, sizeof(match1));
    memset(match2, -1, sizeof(match2));
    for(i = 0;i < m;ret += match1[i++] >= 0){
        memset(t, -1, sizeof(t));
        for(s[p=q=0] = i;p <= q&&match1[i] < 0;p++){
            for(k = s[p], j = 0;j < n&&match1[i] < 0;j++){
                if(mat[k][j]&&t[j] < 0){
                    s[++q] = match2[j], t[j] = k;
                    if(s[q] < 0){
                        for(p = j;p >= 0;j = p){
                            match2[j] = k = t[j];
                            p = match1[k];
                            match1[k] = j;
                        }
                    }
                }
            }
        }
    }
    return ret;
}

//#define MAXN 310
//#define _clr(x) memset(x,0xff,sizeof(int)*MAXN)
//int m, n, mat[MAXN][MAXN], match1[MAXN], match2[MAXN], ans;
//int t, temp, T;
//
//int hungary(){
//	int s[MAXN],t[MAXN],p,q,ret=0,i,j,k;
//	for (_clr(match1),_clr(match2),i=0;i<m;ret+=(match1[i++]>=0))
//		for (_clr(t),s[p=q=0]=i;p<=q&&match1[i]<0;p++)
//			for (k=s[p],j=0;j<n&&match1[i]<0;j++)
//				if (mat[k][j]&&t[j]<0){
//					s[++q]=match2[j],t[j]=k;
//					if (s[q]<0)
//						for (p=j;p>=0;j=p)
//							match2[j]=k=t[j],p=match1[k],match1[k]=j;
//				}
//	return ret;
//}

int main(){
    freopen("a.txt", "r", stdin);
    while(scanf("%d", &T)!=EOF){
        while(T--){
            memset(mat, 0, sizeof(mat));
            scanf("%d%d", &m, &n);
            for(int i = 1;i <= m;i++){
                scanf("%d", &temp);
                for(int j = 1;j <= temp;j++){
                    scanf("%d", &t);
                    mat[i-1][t-1] = 1;
                }
            }
            ans = hungary();
            if(ans == m){
                printf("YES\n");
            }else{
                printf("NO\n");
            }
        }
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/ACKOKO/articles/2125437.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值