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

思路:

有n个人,每个人喜欢k个活动,如果两个人有任意一个活动相同,就称为他们处于同一个社交网络。求这n个人一共形成了多少个社交网络。

 
#include <bits/stdc++.h>
using namespace std;
int p[],a[];
int n,k,m,h;
char c;
bool cmp(int a,int b)
{
return a>b;
} int found(int x)
{
if(p[x] == x)
return x;
return found(p[x]);
}//found函数返回的是
void unite(int a,int b)
{
int x = found(a);
int y = found(b);
if(x!=y)
{
p[y] = x;
}
}//unite函数
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
p[i] = i;
for(int i=;i<=n;i++)
{
scanf("%d %c",&k,&c);
for(int j=;j<=k;j++)
{
scanf("%d",&h);
if(a[h] == )
{
a[h] = i;
}
else
{
unite(found(a[h]),i);
}
}
}
int cnt = ;
int count1[] = {};
for(int i=;i<=n;i++)
{
count1[found(i)]++;
}
for(int i=;i<=n;i++)
{
if(count1[i]!=)
cnt++;
}
cout<<cnt<<endl;
sort(count1+,count1+n+,cmp);
for(int i=;i<=cnt;i++)
{
if(i==)
cout<<count1[i];
else
cout<<" "<<count1[i];
}
return ;
}
 

1107 Social Clusters (30 分)的更多相关文章

  1. 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)

    题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...

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

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

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

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

  4. 1107 Social Clusters (30)(30 分)

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

  5. 1107. Social Clusters (30)

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

  6. pat甲级 1107. Social Clusters (30)

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

  7. PAT (Advanced Level) 1107. Social Clusters (30)

    简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  8. PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)

    题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...

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

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

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

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

随机推荐

  1. 软考考点---CPU

    软考考点---CPU         中央处理器(CPU,Central Processing Unit)是一块超大规模的集成电路,是一台计算机的运算核心(Core)和控制核心( Control Un ...

  2. PythonCookBook笔记——数据结构和算法

    数据结构和算法 解包赋值 p = [1, 2, 3] a, b, c = p # _表示被丢弃的值 _, d, _ = p # 可变长解包 *a, b = p # 字串切割解包 line = 'nob ...

  3. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  4. easyui Combotree 怎么加载数据 支持多选

    1.开发环境vs2012 mvc4  c# 2.HTML前端代码 <%@ Page Language="C#" AutoEventWireup="true" ...

  5. mysql 5.6 bug

    https://dev.mysql.com/doc/refman/5.6/en/account-management-sql.html USE mysql; SELECT Host,User FROM ...

  6. Git 忽略上传文件设置 .gitignore exclude

    1.项目根目录下创建.gitignore vi .gitignore #输入需要忽略的文件,支持正则表达式, *.o *.exe*.pyc 可以用git status来检查是否生效 未添加文件前: M ...

  7. 7-2 jmu-python-猜数游戏(10 point(s)) 【python】

    7-2 jmu-python-猜数游戏(10 point(s)) 用户从键盘输入两个整数,第一个数是要猜测的数n(<10),第二个数作为随机种子,随机生成一个1~10的整数,如果该数不等于n,则 ...

  8. 谈谈网站测试中的AB测试方法

    什么是A/B测试? A / B测试,即你设计的页面有两个版本(A和B),A为现行的设计, B是新的设计.比较这两个版本之间你所关心的数据(转化率,业绩,跳出率等) ,最后选择效果最好的版本. A / ...

  9. APTM敏捷性能测试模型

    随着应用系统的日趋复杂,仅在系统测试和验收测试阶段执行性能测试已经不能满足迟早发现和解决系统性能瓶颈的要求,Connie Smith博士和Lloyd Winlliams博士在他们提出 的软件性能工程( ...

  10. DB2删除表分区

    近日,由于部门数据库读库空间过小,提出删除掉两个月之前日志表的分区(数据库分区是按时间月分区),记述如下: 上网搜索资料发现删除表分区大概分这么几步: 1.查询需要删除掉的分区: select t.D ...