HDU 1054

http://acm.hdu.edu.cn/showproblem.php?pid=1054

二分图最少顶点覆盖,模板题,双向边最后结果/2

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std ;
struct node{
    int s,t,nxt ;
}e[10005] ;
int head[5005],vis[5005],match[5005],cnt,n,m ;
int find(int s)
{
    for(int i=head[s] ;i!=-1 ;i=e[i].nxt)
    {
        int tt=e[i].t ;
        if(!vis[tt])
        {
            vis[tt]=1 ;
            if(match[tt]==-1 || find(match[tt]))
            {
                match[tt]=s ;
                return 1 ;
            }
        }
    }
    return 0 ;
}
int max_match()
{
    int ans=0 ;
    memset(match,-1,sizeof(match)) ;
    for(int i=0 ;i<n ;i++)
    {
        memset(vis,0,sizeof(vis)) ;
        ans+=find(i) ;
    }
    return ans ;
}
void add(int s,int t)
{
    e[cnt].s=s ;
    e[cnt].t=t ;
    e[cnt].nxt=head[s] ;
    head[s]=cnt++ ;
}

int main(){
    while(~scanf("%d%*c",&n)){
        memset(head,-1,sizeof(head));
        cnt=0;
        for(int i=0;i<n;i++){
            int s;
            scanf("%d%*c%*c%d%*c",&s,&m);
            for(int j=0;j<m;j++){
                int t;
                scanf("%d%*c",&t);
                add(s,t);add(t,s);
            }
        }
        printf("%d
",max_match()/2);
    } 
    return 0; 
}
View Code
原文地址:https://www.cnblogs.com/xiaohongmao/p/4061765.html