您的位置:首页 > 其它

HDU 1068 Girls and Boys(模板——二分图最大匹配)

2017-08-14 23:24 405 查看
题目链接:

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

[align=left]Problem Description[/align]
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.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

5
2
解题思路:
处理数据,使用匈牙利算法即可。
AC代码:

#include<stdio.h>
#include<string.h>
const int N=1001;
int n,e

,match[2*N],bk
;
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",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;
}
//易错分析
//参见注释处
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: