邻接表二分图匹配的模板

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int nMax=20005;
class edge{
public:
    int v,nex;
};edge e[200005];
int n,m,k,head[nMax];
int link[nMax];
bool vis[nMax];
void addedge(int b,int a){//向图中加边的算法,注意加上的是有向边//b为a的后续节点既是a---->b
    e[k].v=a;
    e[k].nex=head[b];
    head[b]=k;k++;
}
bool dfs(int u){
    for(int i = head[u]; i != 0; i = e[i].nex){
        int v = e[i].v;
        if(!vis[v]){
            vis[v] = true;
            if(link[v] == -1 || dfs(link[v])){
                link[v] = u;
                return true;
            }
        }
    }
    return false;
}
int main(){
    int ans=0,i,j,a,b,c;
    while(scanf("%d",&n)!=EOF){
        k=1;
        memset(head,0,sizeof(head));
        for(i=0;i<n;i++){
            scanf("%d: (%d)",&a, &b);
            a++;
            while(b--){
                scanf("%d",&c);
                c-=(n-1);
                addedge(a,c);
            }
        }
        ans=0;
        memset(link,-1,sizeof(link));
        for(i = 1; i <= n; i ++){
            memset(vis,0,sizeof(vis));
            if(dfs(i)) ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(c,算法,Class,ini)