hdu 1068 Girls and Boys (二分图匹配)

来源:互联网 发布:腾讯数据分析招聘 编辑:程序博客网 时间:2024/06/09 08:42

Girls and Boys

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


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
/*   这题大概意思就是说找出一个最大的集合使得该集合的任意两个人木有关系。    根据最大独立集 =顶点数 - 最大匹配数    由于题目没有给出哪些是男的哪些是女的,也就是说没有明显的二分图,   所以将一个人拆成两个人进行最大匹配。   由于一个拆成两个,所以最大匹配数应该是求出来的数除以2 。*/#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define maxn 1005using namespace std;int n,m,ans;int g[maxn][maxn],cx[maxn],cy[maxn],mk[maxn];int path(int u){    int v;    for(v=0;v<n;v++)    {        if(g[u][v]&&!mk[v])        {            mk[v]=1;            if(!cy[v]||path(cy[v]))            {                cx[u]=v;                cy[v]=u; //             printf("u:%d v:%d\n",u,v);                return 1;            }        }    }    return 0;}void march(){    int i,j;    ans=0;    memset(cx,0,sizeof(cx));    memset(cy,0,sizeof(cy));    for(i=0;i<n;i++)    {        if(!cx[i])        {            memset(mk,0,sizeof(mk));            ans+=path(i);        }    }}int main(){int i,j,t,temp,s;while(~scanf("%d",&n)){    memset(g,0,sizeof(g));for(i=1;i<=n;i++)        {            scanf("%d",&s);            scanf("%*c%*c%*c%d%*c",&m);            for(j=1;j<=m;j++)            {                scanf("%d",&temp);                g[s][temp]=g[temp][s]=1;            }        }        march();        printf("%d\n",n-ans/2);}return 0;}


 
原创粉丝点击