Girls and Boys
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 10912   Accepted: 4887

Description

In 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.

Input

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 (n <=500 ), for n subjects.

Output

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

field=source&key=Southeastern+Europe+2000">Southeastern Europe 2000



题目链接:http://poj.org/problem?id=1466



题目大意:一些男的和一些女的有的之间有关系,同性之间无关系,求一个最大的集合。使得里面随意两人没有关系



题目分析:裸的最大独立集问题,建立二分图,然后依据最大独立集=点数-最大匹配,求解就可以,注意因为二分图是双向建立的。求出的最大匹配要除2

#include <cstdio>
#include <cstring>
int const MAX = 505;
bool g[MAX][MAX];
bool vis[MAX];
int cx[MAX], cy[MAX];
int n; int DFS(int x)
{
for(int y = 0; y < n; y++)
{
if(!vis[y] && g[x][y])
{
vis[y] = true;
if(cy[y] == -1 || DFS(cy[y]))
{
cy[y] = x;
cx[x] = y;
return 1;
}
}
}
return 0;
} int MaxMatch()
{
memset(cx, -1, sizeof(cx));
memset(cy, -1, sizeof(cy));
int res = 0;
for(int i = 0; i < n; i++)
{
if(cx[i] == -1)
{
memset(vis, false, sizeof(vis));
res += DFS(i);
}
}
return res;
} int main()
{
while(scanf("%d", &n) != EOF)
{
memset(g, false, sizeof(g));
for(int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
int num;
scanf(": (%d)", &num);
for(int j = 0; j < num; j++)
{
int y;
scanf("%d", &y);
g[x][y] = true;
}
}
int ans = MaxMatch();
printf("%d\n", n - ans / 2);
}
}

POJ 1466 Girls and Boys (匈牙利算法 最大独立集)的更多相关文章

  1. poj 1466 Girls and Boys(二分图的最大独立集)

    http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submis ...

  2. POJ - 1466 Girls and Boys 二分图+最大独立集

    标题效果:有着n学生,有一些同学之间的特殊关系.. .为了一探究竟m学生.要求m免两者之间的学生有没有这样的特殊关系 解决问题的思路:二分图的问题,殊关系是对称的.所以能够将两个点集都设置为n个点.求 ...

  3. poj 1466 Girls and Boys (最大独立集)

    链接:poj 1466 题意:有n个学生,每一个学生都和一些人有关系,如今要你找出最大的人数.使得这些人之间没关系 思路:求最大独立集,最大独立集=点数-最大匹配数 分析:建图时应该是一边是男生的点, ...

  4. 网络流(最大独立点集):POJ 1466 Girls and Boys

    Girls and Boys Time Limit: 5000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ...

  5. poj 1466 Girls and Boys 二分图的最大匹配

    Girls and Boys Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Descripti ...

  6. POJ 1466 Girls and Boys (ZOJ 1137 )最大独立点集

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=137 http://poj.org/problem?id=1466 题目大意: ...

  7. POJ 1466 Girls and Boys

    Girls and Boys Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Descripti ...

  8. POJ 1466 Girls and Boys 黑白染色 + 二分匹配 (最大独立集) 好题

    有n个人, 其中有男生和女生,接着有n行,分别给出了每一个人暗恋的对象(不止暗恋一个) 现在要从这n个人中找出一个最大集合,满足这个集合中的任意2个人,都没有暗恋这种关系. 输出集合的元素个数. 刚开 ...

  9. poj 1466 Girls and Boys(二分匹配之最大独立集)

    Description In the second year of the university somebody started a study on the romantic relations ...

随机推荐

  1. Swift学习笔记(二)——常量与变量

    这篇博客将会学习到Swift中的常量Constants和变量Variable.这是学习语言的基础.当中能够看到Swift每句后面基本都是没有:分号的,假设有加:分号的习惯,也能够加上. (1)常量声明 ...

  2. 【试水CAS-4.0.3】第02节_CAS服务端登录页个性化

    完整版见https://jadyer.github.io/2015/07/16/sso-cas-login-diy/ /** * @see ------------------------------ ...

  3. MyEclipse常见错误汇总,中英注释版(长期更新)

    No.1 当一条语句漏写分号时错误描述如下 Syntax error, insert ";" to complete Statement(语法错误:插入分号完成语句描述) No.2 ...

  4. 自己封装js组件 - 初级

    2天前抱着试试看的态度注册了此神博,心血来潮呕心沥血写了一篇关于vue 自定义组件的小文章 尼玛果然一个评论的没有!果然毫无人气!(当然了我这文章内容有限和大神们的比起来简直是粗制滥造...)索性我就 ...

  5. 使用caffemodel模型(由mnist训练)测试单张手写数字样本

    caffe中训练和测试mnist数据集都是批处理,可以反馈识别率,但是看不到单张样本的识别效果,这里使用windows自带的画图工具手写制作0~9的测试数字,然后使用caffemodel模型识别. 1 ...

  6. Strings are immutable

    It is tempting to use the [] operator on the left side of an assignment, with the intention of chang ...

  7. vuejs helloworld

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. codeforces 400 D Dima and Bacteria【并查集 Floyd】

    题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...

  9. 51Nod 天堂里的游戏

    多年后,每当Noder看到吉普赛人,就会想起那个遥远的下午. Noder躺在草地上漫无目的的张望,二楼的咖啡馆在日光下闪着亮,像是要进化成一颗巨大的咖啡豆.天气稍有些冷,但草还算暖和.不远的地方坐着一 ...

  10. Bootstrap modal.js 源码分析

    /* ======================================================================== * Bootstrap: modal.js v3 ...