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. JDBC事务机制

    package com.jdbc.test; import java.sql.*; /** * 数据库的引擎必须是innodb */ public class Demo02 { PreparedSta ...

  2. python 函数定义顺序

    #!/usr/bin/python # Hello World def order(): print("haha") print('Hello World!') order()

  3. 使用 adb 命令一次性为多个设备安装 apk

    使用 adb 命令一次性为多个设备安装 apk 原创 2016年07月15日 10:40:53 3154 命令简介 adb install [-lrtsdg] (file) 把包文件推送到设备上并安装 ...

  4. Linux用户及权限

    库:lib 共享库:.so ,shared object, 权限: 用户,获取资源,服务的标识符 组,指派权限,标识符 进程:以某个用户的身份在进行,有属主和属组 安全上下文(security con ...

  5. C 基本运算

    一 算术运算 C语言一共有34种运算符 包括了常见的加减乘除运算 1. 加法运算+ 除开能做加法运算 还能表示正号: +5, +90 2. 减法运算- 除开能做减法运算 还能表示符号: -10, -2 ...

  6. 【WXS全局对象】Math

    Math对象用于执行数学任务. 属性: 名称 说明 Math.E 代表算术常量 e,即自然对数的底数,其值近似于 2.71828. Math.LN10 就是 loge10,即 10 的自然对数,其值近 ...

  7. Struts2(八.添加用户多张照片实现文件上传功能)

    1.modify.jsp 在modify.jsp修改用户信息页面实现文件上传,添加用户照片的功能 如果是文件上传,method必须是post,必须指定enctype <form method=& ...

  8. Java进阶知识点:并发容器背后的设计理念

    一.背景 容器是Java编程中使用频率很高的组件,但Java默认提供的基本容器(ArrayList,HashMap等)均不是线程安全的.当容器和多线程并发编程相遇时,程序员又该何去何从呢? 通常有两种 ...

  9. 5.azkaban权限管理

    权限简介 user 登录azkaban的用户 注意,如果不给用户roles groups,则用户就是普通用户,只能创建\查看\执行\调度自己的任务,不能看别人的 group group:用户的集合,给 ...

  10. POJ 3675 Telescope(简单多边形和圆的面积交)

    Description Updog is watching a plane object with a telescope. The field of vision in the telescope ...