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 ...
随机推荐
- 解析CEPH: 存储引擎实现之一 filestore
Ceph作为一个高可用和强一致性的软件定义存储实现,去使用它非常重要的就是了解其内部的IO路径和存储实现.这篇文章主要介绍在IO路径中最底层的ObjectStore的实现之一FileStore. Ob ...
- BZOJ 1835 [ZJOI2010]base 基站选址:线段树优化dp
传送门 题意 有 $ n $ 个村庄在一排直线上,现在要建造不超过 $ K $ 个通讯基站,基站只能造在村庄处. 第 $ i $ 个村庄距离第 $ 1 $ 个村庄的距离为 $ D_i $ .在此建造基 ...
- extundelete实现Linux下文件/文件夹数据恢复!
我用的是Centos系统,在安装extundelete之前需要安装e2fsprogs,e2fsprogs-libs,e2fsprogs-devel. 这里用:yum install e2fsprogs ...
- mysql 导入表数据中文乱码
方法一: 先在命令行设置为utf8,再导入 1. use database_name; 2. set names utf8; (或其他需要的编码) 3. source example.sql (sql ...
- python标准日志模块logging使用
python的标准库里的日志系统从Python2.3开始支持.只要import logging这个模块即可使用.如果你想开发一个日志系统, 既要把日志输出到控制台, 还要写入日志文件,只要这样使用: ...
- C# 运行时替换方法(需要unsafe编译)
/* https://stackoverflow.com/questions/7299097/dynamically-replace-the-contents-of-a-c-sharp-method ...
- 【Python】小技巧
1. 退出python shell 在windows下,Ctrl + Z退出 在unix下,Ctrl + D退出
- hdu1845
题解: 只要输出n/2即可 代码: #include<cstdio> #include<cmath> #include<cstring> #include<a ...
- Context的作用
context用于访问全局资源 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSta ...
- android实现log日志输出
1.下载android的log4j的库(的封装) 去: http://code.google.com/p/android-logging-log4j/ 下载对应的 android-logging-lo ...