USACO 4.2.2 && POJ 1274 —— 二分图匹配

来源:互联网 发布:武汉福禄网络 编辑:程序博客网 时间:2024/05/04 22:35
The Perfect Stall
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 16733 Accepted: 7659

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 52 2 53 2 3 42 1 53 1 2 51 2 

Sample Output

4

Source

USACO 40

题意是有n头牛,m个牛栏,每个牛都有自己喜欢的牛栏,问你最多可满足多少牛选择自己喜欢的牛栏

匈牙利算法的模版题。

/*ID: xinming2PROG: stall4LANG: C++*/#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>#include <utility>#include <cassert>using namespace std;///#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , mid  , rt << 1#define rson mid + 1 , r , rt << 1 | 1#define mk make_pair#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)const int MAXN = 400 + 50;const int sigma_size = 26;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x7fffffff;const int IMIN = 0x80000000;#define eps 1e-8const int MOD = (int)1e9 + 7;typedef long long LL;const double PI = acos(-1.0);typedef pair<int , int> pi;#define Bug(s) cout << "s = " << s << endl;///#pragma comment(linker, "/STACK:102400000,102400000")int n , m;int V;vector<int>G[MAXN];bool used[MAXN];int match[MAXN];void add_edge(int u , int v){    G[u].push_back(v);    G[v].push_back(u);}bool dfs(int v){    used[v] = true;    for(int i = 0 ;i < G[v].size() ; i++)    {        int u = G[v][i] , w = match[u];        if(w < 0 || !used[w] && dfs(w))        {            match[u] = v;            match[v] = u;//            cout << u << "--" << v <<endl;            return true;        }    }    return false;}int bipartite(){    int res = 0;    memset(match , -1 , sizeof(match));    for(int i = 0 ; i < V ; i++)    {        if(match[i] < 0)        {            memset(used , 0 , sizeof(used));            if(dfs(i))            {//                cout << i << match[i] << endl;                res++;            }        }    }    return res;}int main(){    freopen("stall4.in" , "r" , stdin);    freopen("stall4.out" , "w" , stdout);    while(~scanf("%d%d" , &n , &m))    {        clr(G , 0);        for(int i = 0 ;i < n ; i ++)        {            int num, x;            scanf("%d" , &num);            while(num--)            {                scanf("%d" , &x);//                cout << i << "->" << x - 1 << endl;                add_edge(i , n + x - 1);            }        }//        for(int u = 0 ; u < 5 ; u++)for(int i = 0 ; i< G[u].size() ; i++)cout << u << "___"<< G[u][i] << endl;        V = m + n;        printf("%d\n" , bipartite());    }    return 0;}


原创粉丝点击