Girls and Boys POJ - 1466 【(二分图最大独立集)】
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 【(二分图最大独立集)】的更多相关文章
- Girls and Boys(poj 1466)
题目描述: 给出一系列男女配对意愿信息.求一个集合中的最大人数,满足这个集合中两两的人不能配对. /* 二分图的最大独立集 因为没有给出具体的男生和女生,所以可以将数据扩大一倍,即n个男生,n个女生, ...
- Poj(1466),最大独立集,匈牙利算法
题目链接:http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total S ...
- hdoj 1068 Girls and Boys【匈牙利算法+最大独立集】
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Girls and Boys HDU - 1068 二分图匹配(匈牙利)+最大独立集证明
最大独立集证明参考:https://blog.csdn.net/qq_34564984/article/details/52778763 最大独立集证明: 上图,我们用两个红色的点覆盖了所有边.我们证 ...
- HDU 1068 Girls and Boys(模板——二分图最大匹配)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1068 Problem Description the second year of the univ ...
- poj 1466 Girls and Boys(二分图的最大独立集)
http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total Submis ...
- POJ 1466 Girls and Boys (匈牙利算法 最大独立集)
Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10912 Accepted: 4887 D ...
- poj 1466 Girls and Boys 二分图的最大匹配
Girls and Boys Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Descripti ...
- POJ 1466:Girls and Boys 二分图的最大点独立集
Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 11097 Accepted: 4960 D ...
随机推荐
- PAT甲级 树 相关题_C++题解
树 目录 <算法笔记>重点摘要 1004 Counting Leaves (30) 1053 Path of Equal Weight (30) 1079 Total Sales of S ...
- SysTick 定时实验(非中断)
实验目的:利用 SysTick 产生 1s 的时基,LED 以 1s 的频率闪烁. 编程要点 1.设置重装载寄存器的值 2.清除当前数值寄存器的值 3.配置控制与状态寄存器 过程 我们创建了两个文件: ...
- Solr介绍 入门练习
1.1 Solr是什么 Solr是一个基于全文检索的企业级应用服务器. 全文检索:可以输入一段文字,通过分词检索数据!!(复习) 应用服务器:它是单独的服务. 1.2 Solr能做什么 它就是用于做全 ...
- [二叉树算法]让树所有叶子节点连成一个单链表,让rchild作为 next指针
//让树所有叶子节点连成一个单链表,让rchild作为 next指针 LNode *head=null,*pre=null;//全局变量 LNode *InOrder(BTNode *T){ if(T ...
- Eureka常见问题
一 Eureka注册慢问题默认情况下,服务注册到Eureka Server过程较慢.在开发或测试时,常常希望加速这一过程,从而提高工作效率.服务注册涉及到周期性心跳,默认30秒一次.只有当实例.服务端 ...
- JAVA的转义字符
一.常见的转义字符 转移字符对应的英文是escape character , 转义字符串(Escape Sequence) 字母前面加上捺斜线"\"来表示常见的那些不能显示的AS ...
- 组装技术的新进展 New advances in sequence assembly.
组装技术的新进展 1.测序和组装 很难想象今天距离提出测序和组装已经有40年啦.我们回头来看一下这个问题. “With modern fast sequencing techniques and su ...
- 路由器开源系统openwrt配置页面定制
1. 新建虚拟机,百度文库有一篇<使用VMware安装OpenWrt>,地址:http://wenku.baidu.com/link?url=NkvaQpTf2dR8FpYn7JD9A7- ...
- [NOIP2018模拟赛10.18]自闭报告
闲扯 这一天,菜鸡RyeCatcher又想起来了被毒瘤题支配的恐惧 今天比较好玩,还是ljy提醒才发现文件夹里有题面...不知道外面的人什么时候才发现 看完了题面,又回到了雅礼啥题也不会写的感觉 T1 ...
- Linux Jetbrains Datagrip 图标找不到
问题 系统版本:Ubuntu 18.04,Datagrip版本:2019.2 从官网下载的 tar.gz 解压之后,找到 datagrip.sh 就可以使用了.但是有个问题,它没有自动创建快捷方式,所 ...