二分图匹配入门专题1B - Girls and Boys hdu1068最大独立集

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二分图匹配入门专题1B - Girls and Boys hdu1068最大独立集相关的知识,希望对你有一定的参考价值。

 

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. 

Input

7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0

Output

5
2

Sample Input

7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0

Sample Output

5
2

 

 

 

题意:输入n,再输入n行数字,每行第一个数字为学生编号,第二个数字为匹配学生的总数k,再输入k个匹配学生编号,问最小的没有匹配的学生数为多少?

思路:这是一个求最大独立集的问题。最大独立集 = 顶点个数-最小顶点覆盖(最大匹配)。由于题目只给了匹配学生编号,没有分性别给出,所以一定会有重复,比如样例1中0和4匹配了两次,所以我们求出的“最大匹配”除以2后才是实际的最大匹配,去重。

 

#include<stdio.h>
#include<string.h>
#define N 1000
int n,m;
int e[N][N];
int book[N],match[N];

int dfs(int u)
{
    int i;
    for(i = 0; i < n; i ++)
    {
        if(!book[i]&&e[u][i])
        {
            book[i] = 1;
            if(!match[i]||dfs(match[i]))
            {
                match[i] = u;
                //match[u] = i;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int i,j,t1,t2,sum,d;
    char a,b,c,f;
    while(scanf("%d",&n)!=EOF)
    {
        memset(e,0,sizeof(e));
        memset(match,0,sizeof(match));
        sum = 0;
        for(i = 0; i < n; i ++)
        {
            scanf("%d%c%c",&t1,&a,&f);//printf("t1=%d\n",t1);
            scanf("%c%d%c",&b,&m,&c);//printf("m=%d\n",m);
            for(j = 0; j < m; j ++)
            {
                scanf("%d",&t2);//printf("t2=%d\n",t2);
                e[t1][t2] = 1;
                //e[t2][t1] = 1;
            }
        }
        for(i = 0; i < n; i ++)
        {
            memset(book,0,sizeof(book));
            if(dfs(i))
                sum ++;
        }
        printf("%d\n",n-sum/2);
    }
    return 0;
 } 
 
 


 

 

 

以上是关于二分图匹配入门专题1B - Girls and Boys hdu1068最大独立集的主要内容,如果未能解决你的问题,请参考以下文章

hdu 1068 Girls and Boys 二分图的最大匹配

Girls and Boys HDU - 1068 二分图匹配(匈牙利)+最大独立集证明

HDU 1068 Girls And Boys 二分图题解

hdu 1068 Girls and Boys 最大独立点集 二分匹配

二分图匹配入门专题1H - Marriage Media light oj 1184二分图最大匹配

Girls and Boys POJ - 1466 (二分图最大独立集)