描述

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.

输入

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, for n students.

输出

For each given data set, the program should write to standard output a line containing the result.

样例输入

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

样例输出

5
2

题意

N个人,求最大子集使得任何两个人都没有亲密关系

题解

先把亲密关系连图,跑匈牙利得到最大匹配,这里不知道男女,所以最大匹配得/2

答案就是N-最大匹配/2

代码

 1 #include<bits/stdc++.h>
 2 #define pb push_back
 3 using namespace std;
 4
 5 const int N=1005;
 6
 7 vector<int> G[N];
 8 int n,match[N],vis[N];
 9 bool dfs(int u)
10 {
11     for(int i=0;i<(int)G[u].size();i++)
12     {
13         int v=G[u][i];
14         if(!vis[v])
15         {
16             vis[v]=1;
17             if(match[v]==-1||dfs(match[v]))
18             {
19                 match[v]=u;
20                 return true;
21             }
22         }
23     }
24     return false;
25 }
26 int hungary()
27 {
28     int ans=0;
29     memset(match,-1,sizeof match);
30      for(int i=0;i<n;i++)
31      {
32         memset(vis,0,sizeof vis);
33         ans+=dfs(i);
34     }
35     return ans;
36 }
37 int main()
38 {
39     while(~scanf("%d",&n))
40     {
41         for(int i=0;i<n;i++)G[i].clear();
42         for(int i=0,x,y,k;i<n;i++)
43         {
44             scanf("%d: (%d)",&x,&k);
45             while(k--)scanf("%d",&y),G[x].pb(y);
46         }
47         printf("%d\n",n-hungary()/2);
48     }
49     return 0;
50 }

转载于:https://www.cnblogs.com/taozi1115402474/p/9526675.html

TZOJ 1321 Girls and Boys(匈牙利最大独立集)相关推荐

  1. poj 1466 Girls and Boys (最大独立集)

    http://poj.org/problem?id=1466 题意:一些 boys 和girls  有暧昧关系,我们要选出 一些人,这些人 任意两个人之间没有暧昧 关系,求最多可以选出 多少人. 题解 ...

  2. (step6.3.2)hdu 1068(Girls and Boys——二分图的最大独立集)

    题目大意:第一行输入一个整数n,表示有n个节点.在接下来的n行中,每行的输入数据的格式是: 1: (2) 4 6 :表示编号为1的人认识2个人,他们分别是4.6: 求,最多能找到多少个人,他们互不认识 ...

  3. hdu1068 Girls and Boys --- 最大独立集

    有一个集合男和一个集合女,给出两集合间一些一一相应关系.问该两集合中的最大独立集的点数. 最大独立集=顶点总数-最大匹配数 此题中.若(a,b)有关.则(b,a)有关.每个关系算了两次,相当于二分图的 ...

  4. HDU 1068 Girls and Boys(最大独立集合 = 顶点数 - 最大匹配数)

    HDU 1068 :题目链接 题意:一些男孩和女孩,给出一些人物关系,然后问能找到最多有多少个人都互不认识. 转换一下:就是大家都不认识的人,即最大独立集合 #include <iostream ...

  5. Girls and Boys||HDU1068

    link:http://acm.hdu.edu.cn/showproblem.php?pid=1068 Problem Description the second year of the unive ...

  6. Girls and Boys

    http://acm.hdu.edu.cn/showproblem.php?pid=1068 题意:在大学校园里男女学生存在某种关系,现在给出学生人数n,并给出每个学生与哪些学生存在关系(存在关系的学 ...

  7. 【HDOJ】1068 Girls and Boys

    匈牙利算法,最开始暴力解不知道为什么就是wa,后来明白,一定要求最优解.查了一下匈牙利算法相关内容,大致了解. 1 #include <stdio.h> 2 #include <st ...

  8. HDU-1068 Girls and Boys

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 the second year of the university somebody start ...

  9. zoj 1137 || 1466 Girls and Boys

    二分图的最大独立集=顶点数-二分图的最大匹配数 二分图的最小顶点覆盖=二分图的最大匹配数 二分图的最小路径覆盖=顶点数-二分图的最大匹配数 zoj上居然没写数据范围...poj写了. 这题是求最大独立 ...

最新文章

  1. 【Java学习笔记之五】java数组详解
  2. 京东智能内容创作算法的演进与实践:基于关键词自动生成摘要
  3. 福利丨吴恩达机器学习新书免费领!
  4. 方法级权限控制-@Secured注解使用
  5. python聚类分析结果很差_python中的聚类分析:value错误:x和y的大小必须相同
  6. [ZJOI2010]贪吃的老鼠
  7. 别再纠结线程池大小 + 线程数量了,没有固定公式的!
  8. easyui表单提交,后台获取不到值
  9. Windows域控 批量设置用户下次登录 修改密码【全域策略生效】
  10. java学习笔记—标准连接池的实现(27)
  11. IE7 下载EXCEL注意地方
  12. 两台服务器ubuntu20.x 直接文件共享,文件挂载 nfs
  13. TensorRT:AttributeError: 'module' object has no attribute 'Logger'
  14. (十八)其他数据库对象,视图,序列
  15. 题8.9:写一函数,将一个3x3的整型矩阵转置。
  16. 网络对战五子棋(来一起PK鸭)
  17. 制作Wi-Fi Ducky远程HID攻击设备
  18. 题外话之怎么脱离旱鸭子
  19. 如何搭建FTP--文件传输协议
  20. Centos7下下载git

热门文章

  1. android webview 加载进度和自定义404错误页面
  2. 【转载】Centos7修改root密码
  3. Thread和Runnable的区别和联系、多次start一个线程会怎么样
  4. [JZOJ5863] 【NOIP2018模拟9.11】移动光标
  5. 空类-自动生成的函数
  6. Android攻城狮SurfaceView
  7. centos中使用python遇到的几个问题
  8. 乐在其中设计模式(C#)系列文章索引
  9. 九、WebService中文件传输
  10. github Android-Universal-Image-Loader