Source:

PAT A1107 Social Clusters (30 分)

Description:

When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. You are supposed to find all the clusters.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (≤), the total number of people in a social network. Hence the people are numbered from 1 to N. Then N lines follow, each gives the hobby list of a person in the format:

K​i​​: h​i​​[1] h​i​​[2] ... h​i​​[K​i​​]

where K​i​​ (>) is the number of hobbies, and [ is the index of the j-th hobby, which is an integer in [1, 1000].

Output Specification:

For each case, print in one line the total number of clusters in the network. Then in the second line, print the numbers of people in the clusters in non-increasing order. The numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

8
3: 2 7 10
1: 4
2: 5 3
1: 4
1: 3
1: 4
4: 6 8 1 5
1: 4

Sample Output:

3
4 3 1

Keys:

Code:

 /*
time: 2019-06-23 14:07:12
problem: PAT_A1107#Social Clusters
AC: 34:25 题目大意:
把一群具有相同爱好的人归为一个社交圈,找出所有的社交圈
输入:
第一行给出,总人数N<=1e3,编号从1~N
接下来N行,给出第i个人的,爱好总数K,各个爱好
输出:
第一行给出,社交圈总数
第二行给出,各个社交圈的人数,从多到少 基本思路:
基于兴趣做并查集操作,
输入每个人的兴趣,首个兴趣的Hash值+1,标记人数
统计父结点个数及其孩子的哈希值即可
*/
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int M=1e3+;
int fa[M],man[M]={},ans[M]={}; int Father(int v)
{
int x=v,s;
while(fa[v] != v)
v = fa[v];
while(fa[x] != x){
s = fa[x];
fa[x] = v;
x = s;
}
return v;
} void Union(int v1, int v2)
{
int f1 = Father(v1);
int f2 = Father(v2);
fa[f2] = f1;
Father(v2);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE for(int i=; i<M; i++)
fa[i]=i; int n,m,h1,h2;
set<int> hobby,clster;
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d:%d", &m,&h1);
man[h1]++;
hobby.insert(h1);
for(int j=; j<m; j++)
{
scanf("%d", &h2);
hobby.insert(h2);
Union(h1,h2);
h1=h2;
}
}
for(auto it=hobby.begin(); it!=hobby.end(); it++){
ans[Father(*it)] += man[*it];
clster.insert(Father(*it));
}
printf("%d\n", clster.size());
sort(ans, ans+M, greater<int>() );
for(int i=; i<clster.size(); i++)
printf("%d%c", ans[i], i+==clster.size()?'\n':' '); return ;
}

PAT_A1107#Social Clusters的更多相关文章

  1. PAT1107:Social Clusters

    1107. Social Clusters (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue When ...

  2. [并查集] 1107. Social Clusters (30)

    1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...

  3. 1107 Social Clusters[并查集][难]

    1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...

  4. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

  5. PAT甲级——1107 Social Clusters (并查集)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30  ...

  6. PAT-1107 Social Clusters (30 分) 并查集模板

    1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...

  7. 1107 Social Clusters——PAT甲级真题

    1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...

  8. 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  9. A1107. Social Clusters

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

随机推荐

  1. Android自己定义控件系列案例【五】

    案例效果: 案例分析: 在开发银行相关client的时候或者开发在线支付相关client的时候常常要求用户绑定银行卡,当中银行卡号一般须要空格分隔显示.最常见的就是每4位数以空格进行分隔.以方便用户实 ...

  2. GTK经常使用控件之笔记本控件( GtkNotebook )

    笔记本控件,能够让用户标签式地切换多个界面. 当我们须要多窗体切换时,使用笔记本控件是一个明智的选择. 笔记本控件的创建: GtkWidget *gtk_notebook_new(void); 返回值 ...

  3. 频繁模式挖掘 Apriori算法 FP-tree

    啤酒 尿布 组合营销 X=>Y,其中x属于项集I,Y属于项集I,且X.Y的交集等于空集. 2类算法 Apriori算法 不断地构造候选集.筛选候选集来挖掘出频繁项集,需要多次扫描原始数据.磁盘I ...

  4. 安装RPM包或者安装源代码包

    第十一章 安装RPM包或者安装源代码包 在windows下安装一个软件非常轻松,仅仅要双击.exe的文件,安装提示连续"下一步"就可以,然而linux系统下安装一个软件似乎并不那么 ...

  5. Java异常&&RuntimeException异常

    Java异常可分为3种: (1)编译时异常:Java.lang.Exception (2)运行期异常:Java.lang.RuntimeException (3)错误:Java.lang.Error ...

  6. B1297 [SCOI2009]迷路 矩阵

    这个题我觉得很有必要写一篇博客.首先,我们需要知道,假如一个邻接矩阵只有0/1构成,那么它自己的n次方就是走n步之后的方案数.但这个题还有2~9咋办呢.我们观察发现,这个题只有10个点,而且边权< ...

  7. shell脚本-循环选择语句

    shell脚本-循环选择语句 过程式编程语言: 顺序执行 选择执行 循环执行 注:条件中的变量,可以在执行语句中使用,不用在加上"$". if语句 根据命令的退出状态来执行命令 单 ...

  8. spring基础学习---简单配置文件

  9. 原生方式实现Ajax技术

    一:什么是Ajax? Ajax:异步的JavaScript和XML,用于完成网页局部刷新功能(修改少量数据只用局部刷新,不用再整个网页重新加载): XML的作用:1.是用于数据传输,但现在都在使用JS ...

  10. Java中的自定义注解

    ## 元注解 要声明一个注解, 我们需要元注解, 元注解是指注解的注解,包括@Retention, @Target, @Document, @Inherited. @Retention 注解的保留位置 ...