HDU 1068 Girls and Boys(最大独立集合)

来源:互联网 发布:c语言郝斌教学视频 编辑:程序博客网 时间:2024/06/04 17:52

Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7555    Accepted Submission(s): 3459


Problem Description
the second year of the university somebody started a study on the romantic relations between the students. The relation “romantically involved” is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been “romantically involved”. The result of the program is the number of students in such a set.

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

the number of students
the description of each student, in the following format
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
or
student_identifier:(0)

The student_identifier is an integer number between 0 and n-1, for n subjects.
For each given data set, the program should write to standard output a line containing the result.
 

Sample Input
70: (3) 4 5 61: (2) 4 62: (0)3: (0)4: (2) 0 15: (1) 06: (2) 0 130: (2) 1 21: (1) 02: (1) 0
 

Sample Output
52
给出0-n-1的人认识的人,求多少个人互不认识。也就是求最大独立集合!
二分图最大独立集合 = 节点数 - 最大匹配数
#include <cstdio>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <algorithm>using namespace std;int xx[501],yy[501];int map[501][501];int v[501];int n;int dfs(int r){    for(int i=0;i<n;i++)    {        if(map[r][i]&&!v[i])        {            v[i]=1;            if(yy[i]==-1 || dfs(yy[i]))            {                 xx[r]=i;                 yy[i]=r;                 return 1;            }        }    }    return 0;}int erfen(){     int ans=0;     memset(xx,-1,sizeof(xx));     memset(yy,-1,sizeof(yy));     for(int i=0;i<n;i++)     {         if(xx[i]==-1)         {             memset(v,0,sizeof(v));             ans+=dfs(i);         }     }     return ans;}int main(){    while(~scanf("%d",&n))    {        //scanf("%d%d",&nx,&ny);        memset(map,0,sizeof(map));        for(int i=1;i<=n;i++)        {            int a,b;            scanf("%d: (%d)",&a,&b);            for(int j=0;j<b;j++)            {                int t;                scanf("%d",&t);                map[a][t]=1;            }        }        int ans=erfen();        printf("%d\n",n-ans/2);    }    return 0;}



0 0