1107 Social Clusters
题意:给出n个人(编号为1~n)以及每个人的若干个爱好,把有一个或多个共同爱好的人归为一个集合,问共有多少个集合,每个集合里有多少个人?
思路:典型的并查集题目。并查集的模板init()函数,union()函数,findSet()函数就不多讲了。这里根据爱好来归类,因此,在读入数据时把爱好进行合并。设置数组hobby[],hobby[id]表示编号为id的这个人的一个爱好,如果某个人有多个爱好,只要记录一个就好了;设置数组num[],num[fa]表示以fa为根结点的爱好集合的人数,初始化为0。然后遍历n个人(人的编号的1~n),对于每个人i,因为我们已经记录了这个人的一个爱好(即hobby[i]),故可以找到该爱好所属的集合的根结点(即int fa=FindSet(hobby[i])),然后令num[fa]++即可。统计好每个集合的人数之后,再统计共有多少个集合,只需要遍历所有爱好(爱好的编号为1~1000),记录num[i]不为0的个数即可。
代码:
#include <cstdio> #include <algorithm> using namespace std; ; int hobby[maxn];//hobby[id]记录id的任意一个爱好 };//num[fa]记录以fa为根的集合的结点个数 int father[maxn]; bool cmp(int a,int b){return a>b;} void Init(){ ;i<maxn;i++) father[i]=i; } int FindSet(int a){ int root=a; while(root!=father[root]) root=father[root]; while(a!=father[a]){ int tmp=a; a=father[a]; father[tmp]=root; } return root; } void Union(int a,int b){ int setA=FindSet(a); int setB=FindSet(b); if(setA!=setB) father[setB]=setA; } int main() { Init(); int n,k; scanf("%d",&n); ;id<=n;id++){ int pre,curr; scanf("%d:%d",&k,&pre); hobby[id]=pre;//记录第i个人的一个爱好 ;j<k;j++){ scanf("%d",&curr); Union(pre,curr); pre=curr; } } //遍历n个人,统计每个集合的人数 ;id<=n;id++) num[FindSet(hobby[id])]++; //遍历所有爱好,统计集合的个数 ; ;i<maxn;i++) ) cnt++; sort(num,num+maxn,cmp); printf("%d\n",cnt); ;i<cnt;i++){ printf("%d",num[i]); ) printf(" "); } ; }
1107 Social Clusters的更多相关文章
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- 1107 Social Clusters[并查集][难]
1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
- 1107 Social Clusters——PAT甲级真题
1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...
- 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 1107 Social Clusters (30)(30 分)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 1107 Social Clusters (30 分)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- pat甲级 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT 1107 Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find som ...
随机推荐
- scala学习手记15 - 独立对象和伴生对象
上一节中的单例对象MarkerFactory 就是一个独立对象的例子.尽管它管理着Marker类,但是它并没有关联到任何类上. scala也可以创建关联到类上的对象.这样的对象同类共享同一个名字,这样 ...
- 将hibernate.cfg.xml文件都放到spring中时报错
报错如下所示: 私以为是配置文件出现问题了. <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- Prism初研究之使用Prism实现WPF的MVVM模式
转自:http://www.cnblogs.com/qianzi067/p/5804880.html
- os.path.abs()与os.path.realpath()的一点区别
相同点 1. 两者都是返回绝对路径,如果参数path为空,则返回当前文件所在目录的绝对路径 当前py文件所在的目录是revise print(os.path.abspath("") ...
- ionic2常见问题——解决下载gradle-2.14.1-all.zip太慢或失败
问题描述 当我们写完ionic2项目准备打包app时(暂时介绍android) 执行命令ionic platform add android的时候下载gradle-2.14.1-all.zip太慢,因 ...
- 007——VUE中非常使用的计算属性computed实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C#中的异常捕获机制(try catch finally)
(转自:http://blog.csdn.net/zevin/article/details/6901489) 一.C#的异常处理所用到关键字try 用于检查发生的异常,并帮助发送任何可能的异常.ca ...
- flash代码
Flash常用的动作命令一.Flash中的常用命令:1.在当前帧停止播放 on(release){ stop();} 2.从当前帧开始播放 on(release){ play();} 3.跳到第 10 ...
- PostgreSQL内存配置记录
PostgreSQL内存配置,参考了其他人的总结,再加上自己的一些体会,做个记录. postgresql的内存分配主要由shared_buffers.temp_buffers.work_mem.mai ...
- [Scala]Scala学习笔记一 基础
1. 变量 val定义的值实际上是一个常亮,无法改变其内容 scala> val num = 0 num: Int = 0 scala> num = 2 <console>:1 ...