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) ...
随机推荐
- env - 在重建的环境中运行程序
SYNOPSIS(总览) env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...] DESCRIPTION(描述) 设置环境中的每个NAME为VAL ...
- Java文件编译与反编译:javac命令和javap命令
1.创建一个Test.java文件,并输入内容 public class Test{ private int m; public int inc(){ return m + 1; } } 2.使用ja ...
- python爬虫---实现项目(一) Requests爬取HTML信息
上面的博客把基本的HTML解析库已经说完了,这次我们来给予几个实战的项目. 这次主要用Requests库+正则表达式来解析HTML. 项目一:爬取猫眼电影TOP100信息 代码地址:https://g ...
- 【php】查看扩展的版本
php --ri [扩展名称] 例如: php --ri mongodb mongodb MongoDB support => enabled MongoDB extension version ...
- Xcode导入第三方库图文
Three20这个与facebook亲戚的开源库是蜚声iPhone开发界,很多App都有它的影子,主要是其真得是功能强大.那么如何将Three20库添加到自己的项目中应用呢?一种是Python命令方式 ...
- Hibernate的核心配置
Hibernate的设计思路 Hibernate是一种全自动化管理持久化对象的ORM框架,既提供了完全面向对象的封装完整的对象持久化接口(屏蔽db层的差异化,提升代码可移植性),也提供了操作HQL和S ...
- 关闭 macOS Google Chrome 黑暗模式风格
终端执行命令 defaults write com.google.Chrome NSRequiresAquaSystemAppearance -bool YES 恢复 defaults write c ...
- django--基础操作
Django基础操作 django常用命令 创建django项目 django-admin startproject mysite 创建项目完成以后,文件目录结构为: 修改settings文件内容 A ...
- Django 多个字段关联同一外键
# -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community b ...
- 我的Python分析成长之路1
Python是什么? ...