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 ...
随机推荐
- TimeSpan 结构(struct)
TimeSpan 是结构类型(struct),即值类型,可以通过两个DateTime(struct)之差来获得,如下例子: DateTime departure = new DateTime(2010 ...
- python中的mysql操作
一. 数据库在自动化测试中的应用 存测试数据 有的时候大批量的数据,我们需要存到数据库中,在测试的时候才能用到,测试的时候就从数据库中读取出来.这点是非常重要的! 存测试结果 二. python中的数 ...
- 算法练习5---快速排序Java版
基本思想:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成 ...
- 用shell将时间字符串与时间戳互转
date的详细用户可以参考下面的 http://www.cnblogs.com/xd502djj/archive/2010/12/29/1919478.html date 的具体用法可以查看另外一篇博 ...
- angularJS中directive父子组件的数据交互
angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...
- ASP.NET MVC 中的IResolver<T> 接口
在ASP.NET MVC 的源码一些实体对象(比如 ControllerBuilder,ControllerFactory, Filters, ViewEngines 等)不再直接通过关键字new来创 ...
- 【第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛-J】 强迫症的序列
小A是一个中度强迫症患者,每次做数组有关的题目都异常难受,他十分希望数组的每一个元素都一样大,这样子看起来才是最棒的,所以他决定通过一些操作把这个变成一个看起来不难受的数组,但他又想不要和之前的那个数 ...
- scrapy的简单使用以及相关设置属性的介绍
0. 楔子(一个最简单的案例) 1.scrapy.Spider scrapy.spiders.Spider name allowed_domains start_urls custom_setting ...
- H264的编解码流程?
- [置顶]
Android RadioButton与TextView浪漫约会?
情景一 今天主要实现一个国家与地区切换,就是当我们选中RadioButton时然后将值设置到TextView中,听着这需求应该不难对吧?那么我们就开始约会吧? 看下原型图 准备条件: 首先需要一个ra ...