Poj_1274 The Perfect Stall -二分图裸题

题目:给牛找棚,每个棚只能容一只牛,牛在对应的棚才能产奶,问最多能让几只牛产奶。

/************************************************
Author        :DarkTong
Created Time  :2016/7/31 10:51:05
File Name     :Poj_1274.cpp
*************************************************/

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 200 + 10;
int w[maxn][maxn], n, m;
int Left[maxn];
bool used[maxn];
bool match(int i)
{
    for(int j=1;j<=n;++j) if(w[j][i]&&!used[j])
    {
        used[j] = true;
        if(!Left[j]||match(Left[j]))
        {
            Left[j] = i;
            return true;
        }
    }
    return false;
}
//返回最大匹配数
int hungary()
{
    int res=0;
    memset(Left, 0, sizeof(Left));
    for(int i=1;i<=m;++i)
    {
        memset(used, 0, sizeof(used));
        if(match(i)) res++;
    }
    return res;
}

int main()
{
    int T, cas=1;
    while(scanf("%d%d", &m, &n)==2)
    {
        memset(w, 0, sizeof(w));
        int t, x;
        for(int i=1;i<=m;++i)
        {
            scanf("%d", &t);
            for(int j=1;j<=t;++j)
            {
                scanf("%d", &x);
                w[x][i]=1;
            }
        }
        printf("%d\n", hungary());
    }

    return 0;
}
时间: 2024-11-04 20:50:32

Poj_1274 The Perfect Stall -二分图裸题的相关文章

POJ1274 The Perfect Stall 二分图,匈牙利算法

N头牛,M个畜栏,每头牛只喜欢其中的某几个畜栏,但是一个畜栏只能有一只牛拥有,问最多可以有多少只牛拥有畜栏. 典型的指派型问题,用二分图匹配来做,求最大二分图匹配可以用最大流算法,也可以用匈牙利算法,这里使用匈牙利算法. #include <stdlib.h> #include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string

POJ_1274_The Perfect Stall(二分图匹配)

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19908   Accepted: 9009 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

POJ 1274-The Perfect Stall(二分图_最大匹配)

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19272   Accepted: 8737 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)

P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶.上个星期,农夫约翰刚刚收集到了奶牛们的爱好的信息(每头奶牛喜欢在哪些牛栏产奶).一个牛栏只能容纳一头奶牛,当然,一头奶牛只能在一个牛栏中产奶. 给出奶牛们的爱好的信息,计算最大分配方案. 输入输出

POJ - 1274 The Perfect Stall 二分图 最大匹配

题目大意:有n头牛,m个牛舍,每个牛舍只能装一头牛.给出每头牛可在的牛舍序号,问最多能有多少头牛能分配到牛舍 解题思路:二分图求最大匹配,牛和牛舍分成两个点集进行匹配 #include<cstdio> #include<vector> #include<cstring> using namespace std; const int N = 210; vector<int> cow[N]; int vis[N], link[N]; int n, m; void

Poj_2239 Selecting Courses -二分图裸题

/************************************************ Author :DarkTong Created Time :2016/7/31 15:38:05 File Name :Poj_2339.cpp *************************************************/ //#include <bits/stdc++.h> #include <cstdio> #include <cstring>

POJ 1274--The Perfect Stall【二分图 &amp;&amp; 最大匹配数 &amp;&amp; 水题】

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20795   Accepted: 9386 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17895   Accepted: 8143 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John r