POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】

时间:2021-03-22 06:15:00
The Perfect Stall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23911   Accepted: 10640

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

Source


裸hungary
算法思想就是不停假设一个点是未盖点然后找增广路
贴一些知识
http://www.renfei.org/blog/bipartite-matching.html
 
匹配:在图论中,一个「匹配」(matching)是一个边的集合,其中任意两条边都没有公共顶点。

最大匹配:一个图所有匹配中,所含匹配边数最多的匹配,称为这个图的最大匹配。

完美匹配:如果一个图的某个匹配中,所有的顶点都是匹配点,那么它就是一个完美匹配。显然,完美匹配一定是最大匹配(完美匹配的任何一个点都已经匹配,添加一条新的匹配边一定会与已有的匹配边冲突)。但并非每个图都存在完美匹配。

交替路:从一个未匹配点出发,依次经过非匹配边、匹配边、非匹配边...形成的路径叫交替路。

增广路:从一个未匹配点出发,走交替路,如果途径另一个未匹配点(出发的点不算),则这条交替路称为增广路(agumenting path)。

 

最大匹配数:最大匹配的匹配边的数目
最小点覆盖数:选取最少的点,使任意一条边至少有一个端点被选择
最大独立数:选取最多的点,使任意所选两点均不相连
最小路径覆盖数:对于一个 DAG(有向无环图),选取最少条路径,使得每个顶点属于且仅属于一条路径。路径长可以为 0(即单个点)。

定理1:最大匹配数 = 最小点覆盖数(这是 Konig 定理)
定理2:最大匹配数 = 最大独立数
定理3:最小路径覆盖数 = 顶点数 - 最大匹配数

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=205;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int n,m,s,u,v;
struct edge{
    int v,ne;
}e[N*N<<1];
int h[N],cnt=0;
inline void ins(int u,int v){
    cnt++;
    e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
}
int vis[N],le[N];
bool find(int u){
    for(int i=h[u];i;i=e[i].ne){
        int v=e[i].v;
        if(!vis[v]){
            vis[v]=1;
            if(!le[v]||find(le[v])){
                le[v]=u;
                return true;
            }
        }
    }
    return false;
}
int ans=0;
void hungary(){
    memset(le,0,sizeof(le));
    for(int i=1;i<=n;i++){
        memset(vis,0,sizeof(vis));
        if(find(i)) ans++;    
    }
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        cnt=0;
        memset(h,0,sizeof(h));
        for(int i=1;i<=n;i++){
            s=read();
            while(s--){v=read();ins(i,v);}
        }
        ans=0;
        hungary();
        printf("%d\n",ans);
    }
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=205;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int n,m,s,u,v,g[N][N];
int vis[N],le[N];
bool find(int u){
    for(int i=1;i<=m;i++) if(g[u][i]&&!vis[i]){
        vis[i]=1;
        if(!le[i]||find(le[i])){
            le[i]=u;
            return true;
        }
    }
    return false;
}
int ans=0;
void hungary(){
    memset(le,0,sizeof(le));
    for(int i=1;i<=n;i++){
        memset(vis,0,sizeof(vis));
        if(find(i)) ans++;    
    }
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(g,0,sizeof(g));
        for(int i=1;i<=n;i++){
            s=read();
            while(s--){v=read();g[i][v]=1;}
        }
        ans=0;
        hungary();
        printf("%d\n",ans);
    }
}