杭电1068 Girls and Boys

来源:互联网 发布:家庭网络如何共享 编辑:程序博客网 时间:2024/05/24 07:33

这是一道二分图的题,把男生女生都看作是两集合里的数,因为两集合中都是所有的人,他们的缘分是双向的,最后搜出来的最大匹配数除以二。由最大独立集=顶点数-最大匹配数,求出最大独立集!!!

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
代码:

//杭电1068
#include<iostream>
using namespace std;
bool map[1002][1002],visit[1002];
int match[1002];
int n;
bool dfs(int k)
{
    int i;
    for(i=0;i<n;i++)
    {
        if(map[k][i] && !visit[i])
        {
            visit[i]=true;
            if(match[i]==-1 || dfs(match[i]))
            {
                match[i]=k;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int k,b,l,i;

    int s;
    while(scanf("%d",&n)!=EOF)//总共有n个人
    {
        s=0;
        memset(map,false,sizeof(map));
        memset(match,-1,sizeof(match));
        for(i=0;i<n;i++)  
        {
            scanf("%d: (%d)",&l,&k);   //l所输入人的编号
              //该人有k个有缘人
        
            while(k--)
            {
                scanf("%d",&b);
                map[l][b]=true;
            }
        }
        for(i=0;i<n;i++)
        {
            memset(visit,false,sizeof(visit));
            if(dfs(i))
                s++;
        }
        
        s=n-s/2;  //最大独立集=顶点数-最大匹配数/2(因为两个集合都是全部人数,他们的缘分是双向的,所以搜出的最大匹配数要除以2)

        printf("%d\n",s);
    
    
    }
    return 0;
}