COGS——T 886. [USACO 4.2] 完美的牛栏

http://www.cogs.pro/cogs/problem/problem.php?pid=886

★★☆   输入文件:stall4.in   输出文件:stall4.out   简单对比
时间限制:1 s   内存限制:128 MB

USACO/stall4(译by Felicia Crazy)

描述

农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术。不幸的是,由于工程问题,每个牛栏都不一样。第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶。上个星期,农夫约翰刚刚收集到了奶牛们的爱好的信息(每头奶牛喜欢在哪些牛栏产奶)。一个牛栏只能容纳一头奶牛,当然,一头奶牛只能在一个牛栏中产奶。

给出奶牛们的爱好的信息,计算最大分配方案。

格式

PROGRAM NAME: stall4

INPUT FORMAT:

(file stall4.in)

第一行 两个整数,N (0 <= N <= 200)和M (0 <= M <= 200)。N是农夫约翰的奶牛数量,M是新牛棚的牛栏数量。
第二行到第N+1行

一共N行,每行对应一只奶牛。第一个数字(Si)是这头奶牛愿意在其中产奶的牛栏的数目(0 <= Si<= M)。后面的Si个数表示这些牛栏的编号。牛栏的编号限定在区间(1..M)中,在同一行,一个牛栏不会被列出两次。

OUTPUT FORMAT:

(file stall4.out)

 只有一行。输出一个整数,表示最多能分配到的牛栏的数量。

SAMPLE INPUT (file stall4.in)

5 5

2 2 5

3 2 3 4

2 1 5

3 1 2 5

1 2

SAMPLE OUTPUT (file stall4.out)

4

二分图最大匹配、、

 1 #include <cstring>
 2 #include <cstdio>
 3 
 4 const int N(233);
 5 int n,m,ans;
 6 bool vis[N];
 7 int match[N],map[N][N];
 8 
 9 bool find(int u)
10 {
11     for(int v=1;v<=m;v++)
12         if(!vis[v]&&map[u][v])
13         {
14             vis[v]=1;
15             if(!match[v]||find(match[v]))
16             {
17                 match[v]=u;
18                 return 1;
19             }
20         }
21     return 0;
22 }
23 
24 inline void read(int &x)
25 {
26     x=0; register char ch=getchar();
27     for(;ch>'9'||ch<'0';) ch=getchar();
28     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
29 }
30 
31 int AC()
32 {
33     freopen("stall4.in","r",stdin);
34     freopen("stall4.out","w",stdout);
35     
36     read(n),read(m);
37     for(int k,u=1;u<=n;u++)
38     {
39         read(k);
40         for(int v;k--;)
41             read(v),map[u][v]=1;
42     }
43     for(int i=1;i<=n;i++)
44     {
45         if(find(i)) ans++;
46         memset(vis,0,sizeof(vis));
47     }
48     printf("%d
",ans);
49     return 0;
50 }
51 
52 int Hope=AC();
53 int main(){;}
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/7466949.html