HDU 1068

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

应用匈牙利算法第三个扩展,求二分图的最大独立集,但由于路径是双向的,所以求出的最大匹配是实际最大匹配数*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",&n))
    {
        memset(head,-1,sizeof(head)) ;
        cnt=0 ;
        for(int i=0 ;i<n ;i++)
        {
            scanf("%*d%*c%*c%*c%d%*c",&m) ;
            //printf("%d!!
",m) ;
            for(int j=0 ;j<m ;j++)
            {
                int t ;
                scanf("%d",&t) ;
                add(i,t+n) ;
            }
        }
        printf("%d
",n-max_match()/2) ;
    } 
    return 0 ; 
}
View Code
原文地址:https://www.cnblogs.com/xiaohongmao/p/3666543.html