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 ...
随机推荐
- JNI_Z_02_函数参数_JNIEnv*_jclass_jobject
1. 1.1.JNIEXPORT void JNICALL Java_包名_类名_函数名01(JNIEnv * env, jclass clazz) // Java代码中的 静态函数 1.2.JNIE ...
- [Jquery 插件]活动倒计时,可同步服务器时间,倒计时格式随意设置
活动倒计时,可同步服务器时间,倒计时格式随意设置 使用说明 /* #活动倒计时,可同步服务器时间 startTime:起始时间 endTime:结束时间 format_str:字符模板 speed:倒 ...
- js关闭浏览器窗口及检查浏览器关闭事件
js关闭浏览器窗口,不弹出提示框.支持ie6+,火狐,谷歌等浏览器,下面以一个示例为大家详细介绍下具体的实现方法,感兴趣的朋友可以参考下 js关闭浏览器窗口 js关闭浏览器窗口,不弹出提示框.支持 ...
- idea的常用设置
1.官网 官网:http://www.jetbrains.com/idea/download/#section=windows 官方文档:http://www.jetbrains.com/help/i ...
- iptables(二)iptables实际操作之规则查询
如果你是一个新手,在阅读如下文章时,请坚持读到最后,读的过程中可能会有障碍,但是在读完以后,你会发现你已经明白了. 在进行iptables实验时,请务必在测试机上进行. 之前在iptables的概念中 ...
- day5-time & datetime模块
1.概述 程序设计开发过程中,往往存在很多场景,需要把时间和日期以某种特定形式格式化输出,在python中我们需要借助time & datetime模块来实现,今天就来一探这两大模块的究竟. ...
- Solr快速入门
1. 什么是Solr Solr是基于lucene的全文检索服务器.不同于lucene工具包,solr是一个web应用,运行在servlet容器,屏蔽了底层细节,并对外提供服务. 点我lucene快速入 ...
- Python基础学习(第2天)
第三课:序列(sequence) 1.序列是一种有顺序的元素的集合 序列可以包含1个或多个元素,也可以不包括任何元素: 序列中的元素可以是[基础数据类型]中任一种,也可以是[别的序列]. s1 = ( ...
- 剑指offer--34.数字在排序数组中出现的次数
时间限制:1秒 空间限制:32768K 热度指数:209611 本题知识点: 数组 题目描述 统计一个数字在排序数组中出现的次数. class Solution { public: int GetNu ...
- L134
这种成功和后来的研究(表明记忆本身并不是先天决定的)使爱立信总结到,记忆的行为与其说是一种习得的行为不如说是一种先天的行为. 这点我们不清楚-构思物体和找出数字模型的能力,回答问题(最好的诗人和哲学家 ...