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 ...
随机推荐
- 抓jsoup_02_数据
1.测试网页:http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-1.html ZC: 直接查看的话,使用这个链接:http ...
- java-ConcurrentLinkedQueue 简单使用
import java.util.concurrent.ConcurrentLinkedQueue; public class CacheTest { /** * * offer(E e) 将指定元素 ...
- 算法练习5---快速排序Java版
基本思想:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成 ...
- python:一个比较有趣的脚本
宿舍火星wifi经常掉,然后要重启,于是用Python写了一个脚本,用来远程控制火星wifi的重启 思路: 01.使用socket通讯 02.在wifi主机(开wifi的电脑)上运行客户端,控制机运行 ...
- FFT快速傅立叶变换
//最近突然发现博客园支持\(\rm\LaTeX\),非常高兴啊! 话说离省选只有不到五天了还在学新东西确实有点逗…… 切到正题,FFT还是非常神奇的一个东西,能够反直觉地把两个多项式相乘的时间复杂度 ...
- Cannot run program "svn" (in directory "G:\IntelliJ IDEA 2017.2.5"): CreateProcess error=2, 系统找不到指定的文件。
- 如何使用google搜索
作者:崔凯链接:https://www.zhihu.com/question/20161362/answer/14180620来源:知乎著作权归作者所有,转载请联系作者获得授权. 搜索引擎命令大全! ...
- [Hive]HiveServer2配置
HiveServer2(HS2)是一个服务器接口,能使远程客户端执行Hive查询,并且可以检索结果.HiveServer2是HiveServer1的改进版,HiveServer1已经被废弃.HiveS ...
- Android代码混淆及项目发布方法记录
Android代码混淆及项目发布步骤记录 本来整理了一份Android项目混淆与发布的文档,突然想到何不写篇博客,分享一下呢,如是便有了本文. Android代码混淆及项目发布步骤记录 一.清理 ...
- ajax 提交 注册表单 到MySQL数据库
今天按照要求,要做一个登陆.注册表单,本来样式做好就行了,本来咱就是干前端的,但让咱自己都没想到的是,不到一个小时竟然都干完了,实在闲的蛋疼,就想到链接数据库玩,遥想当年,毕竟咱也是写过后台的,哪知, ...