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 ;
}

最新文章

  1. 谈一下如何设计Oracle 分区表
  2. 通过Gulp使用Browsersync实现浏览器实时响应文件更改
  3. 阿里云CentOS6.5搭建服务器JDK+tomcat+MySQL
  4. PSP
  5. ASP.NET CORE 1.0 MVC API 文档用 SWASHBUCKLE SWAGGER实现
  6. 分治法避免定义多个递归函数,应该使用ResultType
  7. TXT文件去除多余空行
  8. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON Cast 使用方式
  9. 戏说PHP的嵌套函数
  10. shell if判断的种类
  11. [Everyday Mathematics]20150109
  12. AngularJS学习篇(十五)
  13. IOS KVO没有在delloc中移除导致奔溃
  14. web前端不可错过的开发工具–Adobe Brackets
  15. LDO与DC-DC
  16. kafka杂记
  17. SQL语言类别
  18. Visual Studio解决方案vs2005/vs2008/vs2010/vs2012/vs2013/vs2015版本互相转换工具
  19. P4 Runtime和p4 info
  20. 5G信令(就是用户身份信息)风暴——就是客户端通过公钥加密的消息(携带手机IMSI号)发给服务端,服务器需用私钥解密,这个解密比较消耗资源,如果短时间大量请求到来就会触发信令风暴

热门文章

  1. Error: errCode: -404011 cloud function execution error | errMsg: clou……错误
  2. Python--读取数据库
  3. Mysql高可用集群环境介绍
  4. aspnet core 全局模型验证,统一api响应
  5. 经典SQL数据库面试题以及答案—Oracle版本-SQL全部在plsql开发编写-欢迎提问
  6. java 框架-模板引擎FreeMarker
  7. 【web】使用ionic搭建移动端项目 icon-radio 标签在ios下全部选中的问题
  8. Python练习_购物车_day6
  9. 用Altium Designer16 绘制STM32开发板PCB 笔记
  10. JS实现异步的几种方式