• 【二分图匹配入门专题1】B


    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
    ",t1);
                scanf("%c%d%c",&b,&m,&c);//printf("m=%d
    ",m);
                for(j = 0; j < m; j ++)
                {
                    scanf("%d",&t2);//printf("t2=%d
    ",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-sum/2);
        }
        return 0;
     } 
     
     
    
    
     
  • 相关阅读:
    第四次课堂作业
    12周课后作业
    12周上机(5.21)
    11周周五课后作业
    11周上机作业(5.14)
    第十周(5.7)上机
    第九周4.30上机作业
    第八周周五课后作业
    4.23 第八周上机作业
    4.17课后作业
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7350503.html
Copyright © 2020-2023  润新知