【图算法之二分图】HDU 1068---Girls and Boys

来源:互联网 发布:海思科技人工智能 编辑:程序博客网 时间:2024/05/29 07:24

Girls and Boys

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


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
 题意:就是求出剩余的最少的无关组数(总组数减去情侣总数)。因为题中没告知是男还是女,所以最后求出的最大组数
应该除以二。
#include<stdio.h>#include<string.h>int map[501][501],flag[501],pre[501],num;int get(int i){    int j,k,l;    for(j=0;j<num;j++)    {        if(map[i][j]&&flag[j]==0)        {            flag[j]=1;            if(pre[j]==-1||get(pre[j]))            {                pre[j]=i;                return 1;            }        }    }    return 0;}int main(){    int l,r,i,ll,j,k;    while(~scanf("%d",&num))    {        memset(map,0,sizeof(map));        memset(pre,-1,sizeof(pre));        for(i=0;i<num;i++)        {            scanf("%d: (%d)", &k, &ll);            for(j=0;j<ll;j++)            {                scanf("%d",&r);                map[i][r]=1;            }        }        int sum=0;        for(int i=0;i<num;i++)        {            memset(flag,0,sizeof(flag));            sum+=get(i);        }        printf("%d\n",num-sum/2);    }    return 0;}


0 0
原创粉丝点击