【图论】【二分图匹配】[BZOJ 1085]完美的牛栏

二分图匹配裸体,模板题。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 500;
const int MAXM = 50000;
struct node{
    int v;
    node *next;
}Edges[MAXM*2+10], *ecnt=Edges, *adj[MAXN+10];
int n, m, con[MAXN+10];
bool vis[MAXN+10];
void addedge(int u, int v){
    ++ecnt;
    ecnt->v = v;
    ecnt->next = adj[u];
    adj[u] = ecnt;
}
bool dfs(int u){
    if(vis[u]) return false;
    vis[u] = true;
    for(node *p=adj[u];p;p=p->next){
        int v = p->v;
        if(!vis[v]){
            if(dfs(con[v]) || !con[v]){
                con[u] = v;
                con[v] = u;
                return true;
            }
        }
    }
    return false;
}
int Edmonds(){
    int ans=0;
    for(int i=1;i<=n;i++) if(!con[i]){
        memset(vis, 0, sizeof vis);
        ans += dfs(i);
    }
    return ans;
}
int main(){
    int L, t;
    scanf("%d%d", &n, &m);
    for(int i=1;i<=n;i++){
        scanf("%d", &L);
        for(int j=0;j<L;j++){
            scanf("%d", &t);
            addedge(i, n+t);
            addedge(n+t, i);
        }
    }
    printf("%d\n", Edmonds());

    return 0;
}

posted on 2015-07-27 14:39  JeremyGuo  阅读(155)  评论(0编辑  收藏  举报

导航