Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6867    Accepted Submission(s): 3083

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
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1507 1528 1054 1498 1083 
 

二分匹配入门题:

给出邻接矩阵和邻接表的做法:

一般数据大时邻接表会快很多。

邻接矩阵:

 //2437MS    4188K    907 B    C++
#include<stdio.h>
#include<string.h>
#define N 1005
int g[N][N];
int match[N];
int vis[N];
int n;
int dfs(int x)
{
for(int i=;i<n;i++)
if(!vis[i] && g[x][i]){
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return ;
}
}
return ;
}
int hungary()
{
int ret=;
memset(match,-,sizeof(match));
for(int i=;i<n;i++){
memset(vis,,sizeof(vis));
ret+=dfs(i);
}
return ret;
}
int main(void)
{
int a,m,b;
while(scanf("%d",&n)!=EOF)
{
memset(g,,sizeof(g));
for(int i=;i<n;i++){
scanf("%d: (%d)",&a,&m);
for(int j=;j<m;j++){
scanf("%d",&b);
g[a][b]=g[b][a]=;
}
}
printf("%d\n",n-hungary()/);
}
return ;
}

邻接表:

 //203MS    268K    1169 B    C++
#include<stdio.h>
#include<string.h>
#define N 1005
struct node{
int v;
int next;
}edge[*N];
int match[N];
int vis[N];
int head[N];
int n,edgenum;
void addedge(int u,int v)
{
edge[edgenum].v=v;
edge[edgenum].next=head[u];
head[u]=edgenum++;
}
int dfs(int x)
{
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].v;
if(!vis[v]){
vis[v]=;
if(match[v]==- || dfs(match[v])){
match[v]=x;
return ;
}
}
}
return ;
}
int hungary()
{
int ret=;
memset(match,-,sizeof(match));
for(int i=;i<n;i++){
memset(vis,,sizeof(vis));
ret+=dfs(i);
}
return ret;
}
int main(void)
{
int a,b,m;
while(scanf("%d",&n)!=EOF)
{
edgenum=;
memset(head,-,sizeof(head));
for(int i=;i<n;i++){
scanf("%d: (%d)",&a,&m);
while(m--){
scanf("%d",&b);
addedge(a,b);
addedge(b,a);
}
}
printf("%d\n",n-hungary()/);
}
return ;
}

hdu 1068 Girls and Boys (二分匹配)的更多相关文章

  1. HDU - 1068 Girls and Boys(二分匹配---最大独立集)

    题意:给出每个学生的标号及与其有缘分成为情侣的人的标号,求一个最大集合,集合中任意两个人都没有缘分成为情侣. 分析: 1.若两人有缘分,则可以连一条边,本题是求一个最大集合,集合中任意两点都不相连,即 ...

  2. HDU 1068 Girls and Boys 二分图最大独立集(最大二分匹配)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 思路: 求一集合满足,两两之间没有恋爱关系 思路: 最大独立点集=顶点数-最大匹配数 这里给出的 ...

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

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

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

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

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

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

  7. hdu1068 Girls and Boys 二分匹配

    题目链接: 二分匹配的应用 求最大独立集 最大独立集等于=顶点数-匹配数 本体中由于男孩和女孩的学号是不分开的,所以匹配数应是求得的匹配数/2 代码: #include<iostream> ...

  8. HDU——1068 Girls and Boys

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu 1068 Girls and Boys (最大独立集)

    Girls and BoysTime Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. nio之netty5应用

    1.netty5和netty4的区别不是很大,但是与netty3差别还是有的.这里不介绍netty4,因为和netty5的方式都差不多.netty5的复杂性相对于netty3要多很多了.基本上架构都被 ...

  2. quartz 核心概念

    一.quartz 核心概念 1.scheduler是一个计划调度器容器,容器里面可以盛放众多的JobDetail和trigger,当容器启动后,里面的每个JobDetail都会根据trigger按部就 ...

  3. LVS Nginx HAProxy

    转自:http://blog.chinaunix.net/uid-27022856-id-3236257.html LVS 优点:1.抗负载能力强.工作在第4层仅作分发之用,没有流量的产生,这个特点也 ...

  4. javaweb(五)——Servlet开发(一)

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  5. 惊喜Skr人,Istio的创始人Shriram Rajagopalan手把手教你如何使用Istio

    Shriram与来自Google.Lyft.IBM和其他公司的社区贡献者们一起并肩作战,积极地向Istio和Envoy项目作贡献.同时,Shriram是IBM的Amalgam8项目的创始成员之一.目前 ...

  6. ansible基础配置使用(一)

    test  test  test

  7. Linux命令应用大词典-第6章 文件处理

    6.1 sort:对文件中的数据进行排序 6.2 uniq:将重复行从输出文件中删除 6.3 cut:从文件每行中输出选定的字节.字符或字段 6.4 comm:逐行比较两个已经排序的文件 6.5 di ...

  8. [JSON].result()

    语法:[JSON].result() 返回:[True | False] 说明:用json字符串创建JSON实例时,如果该json字符串不是合法的json格式,会创建一个空的json实例.但是我们如果 ...

  9. leetcode-最长上升子序列LIS

    转载原文地址:http://www.cnblogs.com/GodA/p/5180560.html 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7, ...

  10. JAVA基础学习之路(二)方法定义,重载,递归

    一,方法的定义: package test; public class test1 { public static void main(String args[]) { int result = ad ...