HDU 1068 Girls and Boys(二分匹配--匈牙利算法)

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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

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

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            match[i]=-1;
        }
        memset(e,0,sizeof(e));
        int t1,t2,u;
        for(int i=0;i<n;i++)
        {
            scanf("%d: (%d)",&t1,&t2);
            for(int j=0;j<t2;j++)
            {
                scanf("%d",&u);
                e[t1][u]=1;
            }
        }
        int sum=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                book[j]=0;
            }
            if(dfs(i)==1)
            {
                sum++;
            }
        }
        printf("%d\n",n-sum/2);
    }
    return 0;
}
时间: 2025-01-02 05:34:47

HDU 1068 Girls and Boys(二分匹配--匈牙利算法)的相关文章

hdu 1068 Girls and Boys(匈牙利算法求最大独立集)

Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7044    Accepted Submission(s): 3178 Problem Description the second year of the university somebody started a study on the roman

HDU 1068 Girls and Boys(最大独立集合 = 顶点数 - 最大匹配数)

HDU 1068 :题目链接 题意:一些男孩和女孩,给出一些人物关系,然后问能找到最多有多少个人都互不认识. 转换一下:就是大家都不认识的人,即最大独立集合 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <math.h> #define init(a) memset(a,

hduoj-----(1068)Girls and Boys(二分匹配)

Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7577    Accepted Submission(s): 3472 Problem Description the second year of the university somebody started a study on the romant

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 思路: 求一集合满足,两两之间没有恋爱关系 思路: 最大独立点集=顶点数-最大匹配数 这里给出的关系,看似有向边,实则是无向边,那么按照二分匹配处理的话,相当于一个人看作两个人来用,最后还是顶点数目-最大二分匹配数 因为之间一个人当作两个人用,二分匹配数目减半,即 = n - match()/2 或者也可以写成 = ( 2n -  match() )/2 更容易理解 题目中n的大小没有说明,最

HDU——1068 Girls and Boys

Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12246    Accepted Submission(s): 5768 Problem Description the second year of the university somebody started a study on the roma

【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students

http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 8 using namespace std; 9 const int maxn=2e2

poj 1466 HDU 1068 Girls and Boys (最大独立集)

Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 11141   Accepted: 4983 Description In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically in

最大二分匹配匈牙利算法的python实现

二分图匹配是很常见的算法问题,一般用匈牙利算法解决二分图最大匹配问题,但是目前网上绝大多数都是C/C++实现版本,没有python版本,于是就用python实现了一下深度优先的匈牙利算法,本文使用的是递归的方式以便于理解,然而迭代的方式会更好,各位可以自行实现. 1.二分图.最大匹配 什么是二分图:二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(

hdu 1068 Girls and Boys(二分图匹配)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int mat[1010][1010]; int link[1010]; int vis[1010]; int n,m; bool find(int u) { int v; for(v=0;v<n;v++) if(mat[u][v]&&!vis[v]