[USACO 4.2.2] The Perfect Stall 完美的牛栏

题目链接

匈牙利算法模板题

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <map> 
  
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define DEBUG(a) cout << (a) << endl
typedef long long ll;
int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {1, -1}, {1, 1}};
int dir4[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
const int INF = 0x3f3f3f3fLL;
const long long LLF = 0x3f3f3f3f3f3f3f3fLL;
const int MAXn = 1e5 + 15;
const int mod = 1e9 + 7;
//priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;

int cnt,n,m,Next[400];//Next[i]=a表示第i个牛栏中是第a头牛
bool mapn[402][402],mark[400];//mapn[i][j]=true;表示第i头牛喜欢第j个牛栏  mark[i]=true;表示第i个牛栏用过了
bool check(int x)
{
    for (int i=1;i<=m;i++)//便利每一个牛栏
    if (mapn[x][i]&&(!mark[i]))//如果第x头牛喜欢这个牛栏 并且这个牛栏是空的
    {
        mark[i]=true;//标记这个牛栏
        if (Next[i]==0||check(Next[i]))//如果这个牛栏没有牛或者可以腾出地方
        {
            Next[i]=x;//更新这个牛栏中是第x头牛
            return true;
        }
    }
    return false;
}
int main()
{//输入
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
    {
        int a;
        scanf("%d",&a);
        for (int j=1;j<=a;j++)
        {
            int b;
            scanf("%d",&b);
            mapn[i][b]=true;
        }
    }
    for (int i=1;i<=n;i++)//遍历每头牛
    {
        for (int j=1;j<=m;j++)//清空标记数组
            mark[j]=false;
        if (check(i)) cnt++;
    }
    printf("%d\n",cnt);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值