zoukankan      html  css  js  c++  java
  • 【二分图匹配入门专题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;
     } 
     
     
    
    
     
  • 相关阅读:
    Django学习笔记之中间件和上下文处理器
    python练手小题(四)
    python练手小题(三)
    python练手小题(二)
    python练手小题(一)
    Django学习笔记之自定义过滤器及标签
    Django学习笔记之模板标签与静态文件
    Django学习笔记之模板变量与模板过滤器
    Django学习笔记之url路由及模板渲染方式
    学习Go语言的9大理由
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7350503.html
Copyright © 2011-2022 走看看