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. poj_2777线段树+位运算

    第一次没想到用位运算,不出意料的T了,,, PS:在床上呆了接近两个月后,我胡汉三又杀回来刷题啦-- #include<iostream> #include<cstdio> # ...

  2. 安卓开发--HttpDemo01

    package com.cnn.httpdemo01; import android.app.Activity; import android.content.Intent; import andro ...

  3. SparkCore基础(一)

    * SparkCore基础(一) 学习Spark,首先要熟悉Scala,当然你说你会Python或者Java能不能玩Spark?能!但是不推荐,首推Scala,因为Scala非常便捷,而且Scala有 ...

  4. PostgreSQL Replication之第七章 理解Linux高可用(4)

    7.4 术语与概念 一组计算机被称为集群.集群内的一台计算机被称为一个节点. 当集群内的节点数量是 N (2,,3,等.) ,那么我们讨论一个N节点的集群. 高可用性软件,传输层和集群管理层都运行于每 ...

  5. java高效判断素数

    java高效判断素数 package solution; public class Prime { // 偶数可以由有两个素数相加得到, 一个偶数可能有多个这样的两个素数, 请寻找到 这样两个素数,让 ...

  6. ui5 call view or method from another view

    // call view or method from another view //# view call // var view2=sap.ui.jsview("ui5d.popup01 ...

  7. Debian9.5 系统Samba配置说明

    Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件.SMB(Server Messages Block,信息服务块)通信协议是微软(Microsoft)和英特尔(Intel)在198 ...

  8. 洛谷 P1209 [USACO1.3]修理牛棚 Barn Repair

    P1209 [USACO1.3]修理牛棚 Barn Repair 题目描述 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. ...

  9. Struts(21)OGNL具体解释

    Struts2 中内置了OGNL表达式的支持,使得Struts2的具有比Struts1更为强大的数据訪问的功能.本文主要解说OGNL的用法.并不会去解说一些原理性的东西.想要了解的朋友能够自己去查阅相 ...

  10. HotSpotVM的Java堆实现浅析#1:初始化

    今天来看看HotSpotVM的Java堆初始化. Universe Java堆的初始化主要由Universe模块来完毕,来看下Universe模块初始化的代码,universe_init. jint ...