zoukankan      html  css  js  c++  java
  • HDU 1068 Girls and Boys(模板——二分图最大匹配)

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=1068

    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
    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
    解题思路:
    处理数据,使用匈牙利算法即可。
    AC代码:
    #include<stdio.h> 
    #include<string.h>
    const int N=1001;
    int n,e[N][N],match[2*N],bk[N]; 
    int maxmatch();
    int path(int u);
    
    int main()
    {
        int i,t,x,m;
        while(scanf("%d",&n) != EOF)
        {
            memset(e,0,sizeof(e));
            for(i=0;i<n;i++)
            {
                scanf("%d: (%d)",&x,&t);
                while(t--)
                {
                    scanf("%d",&m);
                    e[x][m]=1;    
                }
            }
            
            printf("%d
    ",n-maxmatch());//最大点独立集数=顶点数-最大匹配数 
        }
        return 0;
    }
    int maxmatch() 
    {
        int i;
        int ans=0;
        memset(match,-1,sizeof(match));
        for(i=0;i<n;i++)
        {
            if(match[i]==-1)
            {
                memset(bk,0,sizeof(bk));
                  ans += path(i);    
            }
        } 
        return ans;
    }
    int path(int u)
    {
        int i;
        for(i=0;i<n;i++)
        {
            if(e[u][i] && !bk[i])
            {
                bk[i]=1;
                if(match[i]==-1 || path(match[i]))//或而不是且 
                {
                    match[i]=u;
                    match[u]=i;
                    return 1;
                }
            }
        }
        return 0;
    }
    //易错分析
    //参见注释处 
     
  • 相关阅读:
    深入理解javascript原型和闭包(5)——instanceof
    深入理解javascript原型和闭包(4)——隐式原型
    启动mongodb的方式
    继承的几种方式
    npm install express 安装失败
    npm insta --savell和npm install --save-dev的区别
    express笔记之一安装
    mongoVUE安装配置
    nodejs笔记之一简介
    nodejs配置之二NPM配置之gulp使用时出现的错误
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/7361114.html
Copyright © 2011-2022 走看看