Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

【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】
: ()
: ()
: ()
: ()
: ()
: ()
: () : ()
: ()
: ()

【Sample Output】


【题意】

题目给定一些男女生之间相互的romantic关系,要求找出一个最大的集合,使得该集合中的所有男女生之间都不存在romantic关系。

【分析】

一个二分图的最大独立集点数与最大二分匹配个数有直接的关系:

最大独立集点数 = 顶点数 - 最大二分匹配对数

故本题直接转化为求最大二分匹配即可,需要注意的是,题中给出的条件是1指向2,2也会指向1,所以最终算出来的匹配数其实是实际对数的两倍,最终被顶点数减去之前首先需要折半。

基础二分匹配练手题。

 /*
ID: Chen Fan
PROG: hdu1068
LANG: G++
*/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; typedef struct nod
{ int a,b;
} node;
node a[];
int result[],start[],num[];
bool flag[]; bool op(node a,node b)
{
if (a.a==b.a) return a.b<b.b;
else return a.a<b.a;
} bool find(int s)
{
for (int i=;i<num[s];i++)
{
int now=a[start[s]+i].b;
if (!flag[now])
{
flag[now]=true;
if (result[now]==-||find(result[now]))
{
result[now]=s;
return true;
}
}
}
return false;
} int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
int tail=;
for (int i=;i<=n;i++)
{
int x,p;
scanf("%d: (%d",&x,&p);
getchar();
for (int j=;j<=p;j++)
{
int y;
scanf("%d",&y);
tail++;
a[tail].a=x;
a[tail].b=y;
}
}
sort(&a[],&a[tail+],op);
int o=-;
memset(num,,sizeof(num));
for (int i=;i<=tail;i++)
{
if (o!=a[i].a)
{
o=a[i].a;
start[o]=i;
}
num[o]++;
} memset(result,-,sizeof(result));
int ans=;
for (int i=;i<n;i++)
{
memset(flag,,sizeof(flag));
if (find(i)) ans++;
} printf("%d\n",n-ans/);
} return ;
}

HDU 1068 Girls and Boys 二分图最大独立集(最大二分匹配)的更多相关文章

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

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 有n个同学,格式ni:(m) n1 n2 n3表示同学ni有缘与n1,n2,n3成为情侣,求集合 ...

  3. hdu - 1068 Girls and Boys (二分图最大独立集+拆点)

    http://acm.hdu.edu.cn/showproblem.php?pid=1068 因为没有指定性别,所以要拆点,把i拆分i和i’ 那么U=V-M (M是最大匹配,U最大独立集,V是顶点数) ...

  4. HDU 1068 Girls and Boys(最大独立集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 题目大意:有n个人,一些人认识另外一些人,选取一个集合,使得集合里的每个人都互相不认识,求该集合 ...

  5. (step6.3.2)hdu 1068(Girls and Boys——二分图的最大独立集)

    题目大意:第一行输入一个整数n,表示有n个节点.在接下来的n行中,每行的输入数据的格式是: 1: (2) 4 6 :表示编号为1的人认识2个人,他们分别是4.6: 求,最多能找到多少个人,他们互不认识 ...

  6. [HDU] 1068 Girls and Boys(二分图最大匹配)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1068 本题求二分图最大独立点集.因为最大独立点集=顶点数-最大匹配数.所以转化为求最大匹配.因为没有给 ...

  7. hdu 1068 Girls and Boys 二分图的最大匹配

    题目链接:pid=1068">http://acm.hdu.edu.cn/showproblem.php? pid=1068 #include <iostream> #in ...

  8. HDU 1068 Girls And Boys 二分图题解

    版权声明:本文作者靖心.靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

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

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

随机推荐

  1. Fragment和Activity之间通过广播的方式传递数据

    四大组件之间传递数据可以用广播,但是有次面试官说太重了,用eventbus代替.下面的广播传递数据方法仅当学习参考. 1.管理类 /** * 广播管理类:注册广播.注销广播.发送广播 * @autho ...

  2. Inno Setup入门(十)——操作注册表

    有些程序需要随系统启动,或者需要建立某些文件关联等问题,这些都是通过在安装程序中对注册表进行操作的结果.Inno Setup中通过[registry]段实现对注册表的操作. 本段说明: 参数列表: 参 ...

  3. iptables基础知识

    iptables防火墙可以用于创建过滤(filter)与NAT规则.所有Linux发行版都能使用iptables,因此理解如何配置 iptables将会帮助你更有效地管理Linux防火墙.如果你是第一 ...

  4. 【0-1 背包模板】 poj 3624

    先看个未经优化的二维空间dp: #include <iostream> #include <cstdio> #include <cmath> #include &l ...

  5. OpenGL中shader读取实现

    1.需要shader在OpenGL中工作,必须经过如下过程 2.代码实现 /********** * loadshader.h **********/ #pragma once #define _CR ...

  6. GCD应用场景

    1.计算文件大小放在子线程中中计算,计算完了,回到主线程更新UI

  7. phpmyadmin配置方式

    简单的说,phpmyadmin就是一种mysql的管理工具,安装该工具后,即可以通过web形式直接管理mysql数据,而不需要通过执行系统命令来管理,非常适合对数据库操作命令不熟悉的数据库管理者,下面 ...

  8. 为Android系统内置C可执行程序测试Linux内核驱动程序

    在前一篇文章中,我们介绍了如何在Ubuntu上为Android系统编写Linux内核驱动程序.在这个名为hello的Linux内核驱动程序中, 创建三个不同的文件节点来供用户空间访问,分别是传统的设备 ...

  9. wifi 3G 流量

    // //  flowStatis.c //  Test // //  Created by iXcoder on 12-7-19. //  Copyright (c) 2012年 iXcoder. ...

  10. bos项目经验心得(1)

    ---恢复内容开始--- 一.搭建数据库环境: 1.在cmd窗口登录mysql数据库: mysql -uroot -proot   (mysql登录数据库的形式就是 mysql-u用户名 -p密码) ...