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
 #include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>
using namespace std;
int hobby[], father[];
int findFather(int x)
{//查找父亲结点并进行路径压缩
if (x == father[x])
return x;
int temp = findFather(father[x]);
father[x] = temp;
return temp;
}
void unionSet(int a, int b)
{//合并两个集合
int ua = findFather(a), ub = findFather(b);
if (ua != ub)
father[ua] = ub;//这里是关键,即将此位置的father改为最近有共同爱好的人
}
int main() {
int n, m, a;
cin >> n;
for (int i = ; i <= n; ++i)father[i] = i;//初始化
for (int i = ; i <= n; ++i)
{
scanf("%d:", &m);
while (m--)
{
cin >> a;
if (hobby[a] == )//没有人有当前这个爱好
hobby[a] = i;//i作为第一个有该爱好的人
else//有人喜欢该爱好
unionSet(hobby[a], i);//将有同样爱好的两个人合并为一个集合
}
}
vector<int>result(n + , );//储存每个集合的人数
for (int i = ; i < n + ; ++i)
++result[findFather(i)];//向前寻找father
sort(result.begin(), result.end(), [](int a, int b) {return a > b; });
int cnt = ;
for (auto t : result)
if (t != )
cnt++;
cout << cnt << endl;
for (int i = ; i < cnt; ++i)//输出result前cnt个元素(result已经从大到小排序,输出的都是集合个数不为0的)
printf("%s%d", i > ? " " : "", result[i]);
return ;
}

PAT甲级——A1107 Social Clusters的更多相关文章

  1. PAT甲级1107. Social Clusters

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

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

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

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

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

  4. PAT A1107 Social Clusters (30 分)——并查集

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

  5. A1107. Social Clusters

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

  6. PAT_A1107#Social Clusters

    Source: PAT A1107 Social Clusters (30 分) Description: When register on a social network, you are alw ...

  7. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

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

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

  9. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. 发现一个新的远程软件 gotohttp

    之前直到远程桌面连接是TeamViewer 替换的原因是: 被控制端版本 11.0.x (很久以前安装的),而我本地的Teamviewer是 14.x, 去连接,好像提示被控制端的版本太低:本地使用 ...

  2. Job 和 Cronjob 的使用

    Job负责处理任务,即仅执行一次的任务,它保证批处理任务的一个或多个Pod成功结束.而CronJob则就是在Job上加上了时间调度. Job 我们用Job这个资源对象来创建一个任务,我们定一个Job来 ...

  3. leetcode-132-分割回文串②*

    题目描述: 方法一:动态规划 class Solution: def minCut(self, s: str) -> int: min_s = list(range(len(s))) n = l ...

  4. IDEA中@Autowired 注解报错~图文

  5. 移动端图片轮播效果:depth模式总结

    最近公司app改版首页增加了一处轮播图效果,但是跟普通的轮播效果不同,是类似于下图的样式,找了一些兼容移动端的插件以及jQuery源码,总结一下使用心得: 1:jquery源码:缺点是在手机端的滑动很 ...

  6. MTT:任意模数NTT

    MTT:任意模数NTT 概述 有时我们用FFT处理的数据很大,而模数可以分解为\(a\cdot 2^k+1\)的形式.次数用FFT精度不够,用NTT又找不到足够大的模数,于是MTT就应运而生了. MT ...

  7. Codeforces768B-Code For 1-类似线段树-枚举+单点更新or区间更新

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面.  每次把\(n\ ...

  8. CSS:CSS 实例

    ylbtech-CSS:CSS 实例 1.返回顶部 1. CSS 实例 CSS背景 设置页面的背景颜色 设置不同元素的背景颜色 设置一个图像作为页面的背景 错误的背景图片 如何在水平方向重复背景图像 ...

  9. ncurses库的介绍与安装

    Frm: http://blog.csdn.net/Mary_Jane/article/details/50769631 介绍 ncurses(new curses)是一套编程库,它提供了一系列的函数 ...

  10. StringUtils里的isEmpty和isBlank的区别

    这边首先以一个简单的测试代码来解释这两者的区别: @Test void stringTest(){ String a = " "; boolean empty = StringUt ...