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 problems, all the stalls in the new barn are different. For the first week, Farmer John randomly
assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only
assigned to one cow, and, of course, a cow may be only assigned to one stall.

Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.

Input

The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of
the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to
produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.

Output

For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

Sample Input

5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2

Sample Output

4

题意 :农夫新建了牛棚,一个牛栏只能容纳一只牛,而每只牛都有自己喜欢的牛栏,给出每只牛喜欢的牛棚,问最多能够匹配多少头牛。

分析:二分图匹配问题。求出最大分配方案。

题目链接:http://poj.org/problem?id=1274

代码清单:

#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

const int maxv = 200 + 5;

int N,M;
int n,m;
bool vis[maxv];
int match[maxv];
bool graph[maxv][maxv];

void init(){
    memset(match,-1,sizeof(match));
    memset(graph,false,sizeof(graph));
}

bool dfs(int u){
    for(int v=1;v<=M;v++){
        if(!vis[v]&&graph[u][v]){
            vis[v]=true;
            if(match[v]==-1 || dfs(match[v])){
                match[v]=u;
                return true;
            }
        }
    }return false;
}

int max_match(){
    int sum=0;
    for(int i=1;i<=N;i++){
        memset(vis,false,sizeof(vis));
        if(dfs(i)) sum++;
    }return sum;
}

int main(){
    while(scanf("%d%d",&N,&M)!=EOF){
        init();
        for(int i=1;i<=N;i++){
            scanf("%d",&n);
            while(n--){
                scanf("%d",&m);
                graph[i][m]=true;
            }
        }
        printf("%d\n",max_match());
    }return 0;
}

静态邻接表:

#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

const int maxv = 200 + 5;

struct Edge{
    int to,next;
}edge[maxv*maxv];

int N,M;
int n,m,index;
bool vis[maxv];
int match[maxv];
int head[maxv*maxv];

void init(){
    index=1;
    memset(head,0,sizeof(head));
    memset(match,-1,sizeof(match));
}

void add(int u,int v){
    edge[index].to=v;
    edge[index].next=head[u];
    head[u]=index++;
}

bool dfs(int u){
    for(int i=head[u];i!=0;i=edge[i].next){
        int v=edge[i].to;
        if(!vis[v]){
            vis[v]=true;
            if(match[v]==-1 || dfs(match[v])){
                match[v]=u;
                return true;
            }
        }
    }return false;
}

int max_match(){
    int sum=0;
    for(int i=1;i<=N;i++){
        memset(vis,false,sizeof(vis));
        if(dfs(i)) sum++;
    }return sum;
}

int main(){
    while(scanf("%d%d",&N,&M)!=EOF){
        init();
        for(int i=1;i<=N;i++){
            scanf("%d",&n);
            while(n--){
                scanf("%d",&m);
                add(i,m);
            }
        }
        printf("%d\n",max_match());
    }return 0;
}
时间: 2024-10-06 00:31:09

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: 17768   Accepted: 8104 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_1274 The Perfect Stall -二分图裸题

题目:给牛找棚,每个棚只能容一只牛,牛在对应的棚才能产奶,问最多能让几只牛产奶. /************************************************ Author :DarkTong Created Time :2016/7/31 10:51:05 File Name :Poj_1274.cpp *************************************************/ //#include <bits/stdc++.h> #inclu

二分图匹配-匈牙利算法【学习】

首先二分图匹配的基础概念得清楚:二分图: 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图.匹配: 两两不含公共端点的边集合M称为匹配(简单的说就是右边的点只能连一条边)极大匹配: 指在当前已完成的匹配下,无法再通过增加未完成匹配的边的方式来增加匹配的边数最大匹配: 所有极大匹配当中边数最大的一个匹配增广路: 若P是图G中一条连通两个未匹配顶

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

POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

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