二分匹配之最大点集

来源:互联网 发布:red hat linux最新版本 编辑:程序博客网 时间:2024/06/16 00:51

,给你一些边的关系,然后让你判断最大的点集数量是多少,就是用匈牙利匹配算出最大匹配数,然后用点的数量减去最大匹配数的二分之一,注意是二分之一

——————————————————————hdu1068————————————————————

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1500;int e[maxn][maxn],match[maxn],book[maxn];int m,n;int dfs(int u){    int i;    for(i=0;i<n;i++)    {        if(book[i]==0&&e[u][i]==1)        {            book[i] = 1;            if(match[i]==0 || dfs(match[i]))            {                match[i] = u;                return 1;            }        }    }    return 0;}int main(){    int i,j,t1,t2,sum,k,t;    while(scanf("%d",&n)==1)    {        memset(e,0,sizeof(e));//一定要记得清空        sum = 0;        for(i=0;i<n;i++)        {            scanf("%d: (%d) ",&t,&k);            while(k--)            {                scanf("%d",&j);                e[t][j] = 1;            }        }        memset(match,0,sizeof(match));        for(i=0;i<n;i++)        {            memset(book,0,sizeof(book));            if(dfs(i))                sum++;        }        printf("%d\n",n-sum/2);    }}/*0: (3) 4 5 61: (2) 4 62: (0)3: (0)4: (2) 0 15: (1) 06: (2) 0 1*/




0 0
原创粉丝点击