Girls and Boys
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 11694   Accepted: 5230

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

题意:在大学校园里男女学生存在某种关系,现在给出学生人数n,并给出每个学生与哪些学生存在关系(存在关系的学生一定是异性)。现在让你求一个学生集合,这个集合中任意两个学生之间不存在这种关系。输出这样的关系集合中最大的一个的学生人数。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std; vector<int> G[505];
int match[505],used[505];
int n;
void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int u)
{
used[u]=1;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
int w=match[v];
if(w<0||!used[w]&&dfs(w))
{
match[u]=v;
match[v]=u;//匹配的边两端点同时标记
return true;
}
}
return false;
} int bipartite_match()
{
memset(match,-1,sizeof(match));
int res=0;
for(int i=0;i<n;i++)
if(match[i]<0)//先前标记过就不用再标记了
{
memset(used,0,sizeof(used));
if(dfs(i)) res++;
}
return res;
} int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++) G[i].clear();
for(int u=0;u<n;u++)
{
int k,num,v;
scanf("%d: (%d)",&k,&num);
for(int i=0;i<num;i++)
{
scanf("%d",&v);
add_edge(u,v);
}
}
printf("%d\n",n-bipartite_match());
}
return 0;
}

  分析:最大独立集问题,看得这篇介绍http://blog.sina.com.cn/s/blog_6635898a0100lyui.html

他们写的模板最后二分匹配出来后都要除以2,因为每条边都重复算了一次,但是因为我的模板

的问题,不需要除以2,因为同时把匹配的边两顶点都标记了

最大独立集=顶点数-匹配的顶点数/2(我的模板中即bipartite_match()返回的边数)

POJ 1466 大学谈恋爱 二分匹配变形 最大独立集的更多相关文章

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

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

  2. poj 2060 Taxi Cab Scheme (二分匹配)

    Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 D ...

  3. poj 1034 The dog task (二分匹配)

    The dog task Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2559   Accepted: 1038   Sp ...

  4. TTTTTTTTTTTTTTTTT POJ 2226 草地覆木板 二分匹配 建图

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9754   Accepted: 3618 Desc ...

  5. TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想

    Purifying Machine Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5004   Accepted: 1444 ...

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

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

  7. POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】

    链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  8. poj 1274 The Prefect Stall - 二分匹配

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22736   Accepted: 10144 Description Far ...

  9. Guardian of Decency POJ - 2771 【二分匹配,最大独立集】

    Problem DescriptionFrank N. Stein is a very conservative high-school teacher. He wants to take some ...

随机推荐

  1. skiplist(跳表)的原理及JAVA实现

    前记 最近在看Redis,之间就尝试用sortedSet用在实现排行榜的项目,那么sortedSet底层是什么结构呢? "Redis sorted set的内部使用HashMap和跳跃表(S ...

  2. Python_4day

    函数 函数可以用来定义可重复代码,组织和简化 一般来说一个函数在实际开发中为一个小功能 一个类为一个大功能 同样函数的长度不要超过一屏   Python中的所有函数实际上都是有返回值(return N ...

  3. TCP/IP 协议是如何保证数据可靠性的?

    原文: 网络基础:TCP协议-如何保证传输可靠性 TCP协议传输的特点主要就是面向字节流.传输可靠.面向连接.这篇博客,我们就重点讨论一下TCP协议如何确保传输的可靠性的. 确保传输可靠性的方式TCP ...

  4. unittest之一框架、suite

    1.unittest是Python的标准库里的模块,所以在创建测试方法时,需直接导入unittest即可 2.unittest框架的六大模块: 测试用例TestCase 测试套件TestSuit:测试 ...

  5. SQL----Scalar 函数

    UCASE() 函数 UCASE 函数把字段的值转换为大写. SQL UCASE() 语法 SELECT UCASE(column_name) FROM table_name SQL UCASE() ...

  6. LOJ题解#136. 最小瓶颈路 DFS+Kruskal

    题目链接: https://loj.ac/problem/136 思路: 在我的这篇博客中已经讲到什么是最短瓶颈路,同时给出了一个用Kruskal求最短瓶颈路的一个简洁易懂的方法,然而这道题目可以看作 ...

  7. OpenSSL(1)密钥和证书管理

    OpenSSL是一个开源项目,包括密码库和SSL/TLS工具集. 从项目的官方站点可以看到: OpenSSL项目是安全套接字层( secure sockets layer, SSL)和传输层安全( t ...

  8. 基于 Debian 的 Netrunner 19.08 “Indigo” 发布

    Netrunner 19.08版本被称为“Indigo”,基于最近发布的Debian GNU/Linux 10 “Buster”操作系统系列,具有KDE Plasma 5.14.5桌面环境,并附带KD ...

  9. 更改命令行,完全显示hostname

    刚装完一台新服务器,想让命令行的能显示全部的hostname,查阅资料后,将$PS1的参数修改即可 1,echo $PS1 2,将其中的/h换成/H即可 3,我是在/etc/profile中加了一行 ...

  10. 02-spring框架—— IoC 控制反转

    控制反转(IoC,Inversion of Control),是一个概念,是一种思想.指将传统上由程序代码直接操控的对象调用权交给容器,通过容器来实现对象的装配和管理. 控制反转就是对对象控制权的转移 ...