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. ORACLE 的前后台进程

    关于oracle用户进程,服务进程,后台进程 用户进程(User Process) 是一个需要与Oracle Server交互的程序 运行于客户端 当用户运行某个工具或应用程序(如SQL*Plus)时 ...

  2. msql 数据类型

    1.数据类型 #1. 数字: 整型:tinyinit int bigint 小数: float :在位数比较短的情况下不精准 double :在位数比较长的情况下不精准 0.0000012301231 ...

  3. recover函数捕获异常

    package main import ( //"fmt" "time" ) func test () { var m map[string]int m[&qu ...

  4. 并不对劲的THUWC2020

    day -inf 因为一些(不是寒假时长锐减的)小原因,今年(2020)THUWC在去年(2019)就举办了! 这导致某个小弱智只能临阵磨枪了QAQ- day 1 早: 没有看到吕爷,签到.试机. 签 ...

  5. (一)Activiti简介

    一.概念 Activiti项目是一项新的基于Apache许可的开源BPM平台,从基础开始构建,旨在提供支持新的BPMN 2.0标准,包括支持对象管理组(OMG),面对新技术的机遇,诸如互操作性和云架构 ...

  6. Python考试_第一次

    python基础数据类型考试题 考试时间:两个半小时 满分100分(80分以上包含80分及格) 一,基础题. 1. 简述变量命名规范(3分) 答:(1) 变量为数字,字母以及下划线的任意组合,且不能以 ...

  7. mycat 报错 java.lang.OutOfMemoryError: Java heap space

    今天排查mysql的错误日志发现  wrapper.log  中有如下错误日志 INFO   | jvm 1    | 2019/10/20 12:52:31 | java.lang.OutOfMem ...

  8. Go map使用

    前言 map 是在 Go 中将值(value)与键(key)关联的内置类型.通过相应的键可以获取到值. 在一个map里所有的键都是唯一的,而且必须是支持==和!=操作符的类型,切片.函数以及包含切片的 ...

  9. ASIHTTPRequest源码简单分析

      1.前言      ASIHttprequest 是基于CFNetwork的,由于CFNetwork是比较底层的http库,功能比较少,因此,在ASIHttprequest中实现了http协议中比 ...

  10. 6.NIO2-Path、Paths、Files

    NIO.2 jdk1.7中,java对 NIO 极大的扩展,主要增强的是对文件处理 和 文件系统特性的支持 关于其中一些API的使用 public class TestNIO_2_Path_File ...