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
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.
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
5
2
输出这种关系集合中最大的一个的学生人数。
(网上翻译)
#include"stdio.h"
#include"string.h"
#define N 505
int g[N][N];
int mark[N];
int link[N];
int n;
int find(int k) //寻找和能和K节点匹配的右节点
{
int i;
for(i=0;i<n;i++) //遍历数组
{
if(g[i][k]&&!mark[i]) //边存在,且是第一次訪问
{
mark[i]=1;
if(link[i]==-1||find(link[i]))
{ //该点未匹配或者和该点匹配的左节点能够另外匹配
link[i]=k;
return 1;
}
}
}
return 0; //匹配失败
}
int main()
{
int i,j,u,v,m;
while(scanf("%d",&n)!=-1)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
g[i][j]=0;
for(i=0;i<n;i++)
{
scanf("%d: (%d)",&u,&m);
while(m--)
{
scanf("%d",&v);
g[u][v]=1;
}
}
memset(link,-1,sizeof(link)); //记录每一个二分图的右半边节点匹配的左节点标号
int ans=0;
for(i=0;i<n;i++)
{
memset(mark,0,sizeof(mark)); //标记数组。防止反复訪问一个节点
if(find(i))
ans++;
}
printf("%d\n",n-ans/2); //最大独立集点数 = N - 最大匹配数
}
return 0;
}
hdu 1068 Girls and Boys(匈牙利算法求最大独立集)的更多相关文章
- HDU 1068 Girls and Boys(模板——二分图最大匹配)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1068 Problem Description the second year of the univ ...
- HDU 1068 - Girls and Boys
求一个集合最多几个人,其之间任意两人没有暧昧关系. 二分图匹配 最大独立集 = 总点数 - 最大匹配数 匈牙利算法 因为每个同学都在二分图的两侧 当 A与B匹配时,B与A也匹配 所以 所求的最大匹配数 ...
- HDU 1068 Girls and Boys 二分图最大独立集(最大二分匹配)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1068 Girls and Boys(最大独立集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 题目大意:有n个人,一些人认识另外一些人,选取一个集合,使得集合里的每个人都互相不认识,求该集合 ...
- POJ 1466 Girls and Boys (匈牙利算法 最大独立集)
Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10912 Accepted: 4887 D ...
- HDU - 1068 Girls and Boys(二分匹配---最大独立集)
题意:给出每个学生的标号及与其有缘分成为情侣的人的标号,求一个最大集合,集合中任意两个人都没有缘分成为情侣. 分析: 1.若两人有缘分,则可以连一条边,本题是求一个最大集合,集合中任意两点都不相连,即 ...
- HDU 1068 Girls and Boys(最大独立集合 = 顶点数 - 最大匹配数)
HDU 1068 :题目链接 题意:一些男孩和女孩,给出一些人物关系,然后问能找到最多有多少个人都互不认识. 转换一下:就是大家都不认识的人,即最大独立集合 #include <iostream ...
- hdu 1068 Girls and Boys (二分匹配)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU——1068 Girls and Boys
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- Bug的定义和分类
什么是BUG 使用人工或自动手段,来运行或测试某个系统的过程.其目的在于检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别 BUG分类 完全没有实现的功能 基本实现了用户需要的功能,但是运行时 ...
- css--使用的四种方法
前戏 之前学习了HTML相关的知识,也能简单的写一个hello world的页面.但是,只学HTML满足不了我们的需求,而HTML.CSS.JavaScript三者搭配使用才能更好的完成我们需要的效果 ...
- 配置maven报错 the java_home environment variable is not defined correctly ......
the java_home environment variable is not defined correctly This environment variable is needed to r ...
- Fortran中常用函数列表
Y=INT(X) 转换为整数 ALL(所有型态) INTEGER Y=REAL(X) 转换为实数 INTEGER REAL Y=DREAL(X) 取复数实部(倍精度) COMPLEX*16 REAL* ...
- CF993E Nikita and Order Statistics 多项式卷积 快速傅里叶变换
题意: 给你一个数组a1~an,对于k=0~n,求出有多少个数组上的区间满足:区间内恰好有k个数比x小.x为一个给定的数.n<=10^5.值域没有意义. 分析: 大神们都说这道题是一个套路题,真 ...
- Jenkins出现 {"changed": false, "msg": "mkdir: cannot create directory ‘/root/.ansible/tmp/ansible-tmp-1552390265.8-253945919613076’: No space left on device\n", "unreachable": true}
之前部署Jenkins没有遇到过这个问题,百度也找不到相关内容,无奈用过google翻译了一下,显示内存不足,上到服务器查询df -h 发现use% 100% 由于日志等文件导致服务器磁盘空间满了,引 ...
- Django框架基础知识08-表关联对象及多表查询
1.自定义主键字段的创建 AutoFiled(pirmary_key=True) # 一般不会自定义,int类型,自增长 一般不自定义主键. 2.order_by asc desc from djan ...
- Android 图片设置圆角
Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片 方法一: 通过第三方框架Glide实现图片显示有圆角,有三种写法如下: 1.1,第一种实现: RequestOptions ...
- LeetCode(74) Search a 2D Matrix
题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the fo ...
- Dividing coins (01背包)
It’s commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic ...