Problem 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
题意:有n个学生,每个学生都和一些人又关系,找出互相没关系的最多的一群人。

思路:这是一道二分图最大独立集模板题【最大独立集= 点数 - 最大匹配数】注意:最大匹配数需要除以2

【参考博客】

看到之后就可以发现,这是一道非常明显的最大独立集的问题,可以转化为二分图来做,还是最经典的拆点建图,然后根据定理,最大独立集=顶点数-最小点覆盖数。  而对于这道题来说,我们可以发现这个浪漫关系是相互的。

而我们的建图中,按理来说应该是一边是男的点,一边是女的点这样连边,但是题目中没说性别的问题。

只能将每个点拆成两个点,一个当作是男的点,一个当作是女的点了,然后连边。由于关系是相互的,这样就造成了边的重复。也就是边集是刚才的二倍,从而导致了最大匹配变成了二倍。

那么 ,最大独立集=顶点数-最大匹配/2,所以最终答案就呼之欲出了。

AC代码:

 #include<algorithm>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std; #define maxn 666
vector<int> v[maxn];
int vis[maxn];
int match[maxn];
int n;
int dfs(int u){
for(int i=;i<v[u].size();i++){
int temp=v[u][i];
if(vis[temp]==){
vis[temp]=;
if(match[temp]==||dfs(match[temp])){
match[temp]=u;
return ;
}
}
}
return ;
}
int main(){
while(~scanf("%d",&n)){
for(int i=;i<n;i++)
v[i].clear();
int x,m,y;
for(int i=;i<=n;i++){
scanf("%d: (%d)",&x,&m);
for(int j=;j<m;j++){
scanf("%d",&y);
v[x].push_back(y);
//v[y].push_back(x);
}
}
memset(match,,sizeof(match));
int ans=;
for(int i=;i<n;i++){
for(int j=;j<=n;j++)
vis[j]=;
if(dfs(i))
ans++;
}
printf("%d\n",n-ans/);
}
return ;
}

Girls and Boys POJ - 1466 【(二分图最大独立集)】的更多相关文章

  1. Girls and Boys(poj 1466)

    题目描述: 给出一系列男女配对意愿信息.求一个集合中的最大人数,满足这个集合中两两的人不能配对. /* 二分图的最大独立集 因为没有给出具体的男生和女生,所以可以将数据扩大一倍,即n个男生,n个女生, ...

  2. Poj(1466),最大独立集,匈牙利算法

    题目链接:http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total S ...

  3. hdoj 1068 Girls and Boys【匈牙利算法+最大独立集】

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

  4. Girls and Boys HDU - 1068 二分图匹配(匈牙利)+最大独立集证明

    最大独立集证明参考:https://blog.csdn.net/qq_34564984/article/details/52778763 最大独立集证明: 上图,我们用两个红色的点覆盖了所有边.我们证 ...

  5. HDU 1068 Girls and Boys(模板——二分图最大匹配)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1068 Problem Description the second year of the univ ...

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

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

  7. POJ 1466 Girls and Boys (匈牙利算法 最大独立集)

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

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

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

  9. POJ 1466:Girls and Boys 二分图的最大点独立集

    Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 11097   Accepted: 4960 D ...

随机推荐

  1. python中浅拷贝和深拷贝分析

    首先,我们知道Python3中,有6个标准的数据类型,他们又分为可以变和不可变.不可变:Number(数字).String(字符串).Tuple(元组).可以变:List(列表).Dictionary ...

  2. windows通过gcc编译代码

    1.将gcc添加到环境变量 2.检查gcc是否安装成功 cmd下输入gcc –v 3.cd进入需要编译源文件的目录 4.dir查看当前目录下是否有需要编译的文件(linux下用ls) 5.编译文件(H ...

  3. shell习题第21题:计算数字的个数

    [题目要求] 计算文档a.txt中每一行出现数字的个数并且要计算一下整个文档中一共出现了几个数字 例如a.txt如下: sdhhyh776dbbgbfg dhhdffhhhs556644382 运行结 ...

  4. java之理解面向对象

    1.程序设计的三种基本结构 顺序结构 顺序结构表示程序中的各操作是按照它们在源代码中的排列顺序依次执行的 选择结构 选择结构表示程序的处理需要根据某个特定的条件选择其中的一个分支执行.选择结构有单选择 ...

  5. QMetaEnum获取枚举元信息

    QMetaEnum 类提供了一个枚举的元数据.我们可以使用该类的静态模板函数,fromType<enumerator>来获得关于某个枚举的QMetaEnum对象,然后就可以调用该类的成员函 ...

  6. css 动画(一)transform 变形

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 有段时间我是没理清 transform.translate.transition 和 animation之 ...

  7. SQL 不同服务器数据库操作

    https://www.cnblogs.com/lusunqing/p/3660190.html --创建远程链接服务器 execute sys.sp_addlinkedserver @server= ...

  8. RESTful接口开发

    package com.aaaaaa.manager.controller; import org.springframework.beans.factory.annotation.Autowired ...

  9. 前端开发 Vue -1windows环境搭建Vue Node开发环境

    解决几个疑问: 想学习下vue.js,我理解的它是一个前端的框架,主要作用是对数据的处理,和juqery类似,所以不太理解为什么要在nodejs中npm install vue呢?在html文件中引入 ...

  10. SVN配置使用及移植

    使用svn作为配置管理工具及其普遍的用于项目开发中,网上有很多关于svn的原理介绍及命令行管理教程.这里仅仅分享下个人配置及使用的过程,不通过命令行,可简单的上手操作.如有遗漏欢迎留言交流. 配置及使 ...