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 (<=1000), 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:

Ki: hi[1] hi[2] ... hi[Ki]

where Ki (>0) is the number of hobbies, and hi[j] 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 <cstdio>
#include <vector>
#include <algorithm>
using namespace std; vector<int> father(), num();
int cnt;//记录集合数 //sort()从大到小排序比较函数
bool cmp(int a, int b){
return a > b;
} //初始化,每个人属于不同集合
void init(int n){
for(int i = ; i <= n; i++){
father[i] = i;
}
} int GetParent(int x){
if(x == father[x])
return x;
father[x] = GetParent(father[x]);//路径压缩
return father[x];
} void merge(int x, int y){
int fx = GetParent(x);
int fy = GetParent(y);
if(fx != fy){
cnt--;//不属于同一集合,合并之后集合数减一
father[fy] = fx;
}
} int main(){
int n, k, h, i;
int hobby[] = {};
scanf("%d", &n); //调用初始化函数
init(n); cnt = n;//初始化集合数为n //处理输入数据,合并集合
for(i = ; i <= n; i++) {
scanf("%d:", &k);
for(int j = ; j < k; j++) {
scanf("%d", &h);
if(hobby[h] == )
hobby[h] = i;
merge(hobby[h], i);
}
}
for(i = ; i <= n; i++)
//这里一定要注意,要找到i所属集合的根节点,num加一,num[father[i]]++是有问题的,因为
//这棵树深度不一定是2.。。即father[i]不一定是i所属集合的根节点
num[GetParent(i)]++; printf("%d\n", cnt); sort(num.begin(), num.end(), cmp); printf("%d", num[]);
for(i = ; i < cnt ; i++)
printf(" %d", num[i]);
return ;
}
 

pat甲级 1107. Social Clusters (30)的更多相关文章

  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. [并查集] 1107. Social Clusters (30)

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

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

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

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

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

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

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

  7. 1107. Social Clusters (30)

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

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

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

  9. PAT甲级——A1107 Social Clusters

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

随机推荐

  1. 为npm设置代理,解决网络问题

    为npm设置代理,解决网络问题 npm config set proxy=http://127.0.0.1:1080

  2. Linux安装FTP文档服务器

    1.检查是否安装 了vsftpd,如果未安装 则安装vsftpd. 1)查看系统中是否安装了vsftpd,可以通过执行命令 :rpm -qa | grep vsftpd 2)如果没有安装 vsftpd ...

  3. 基于itchat实现微信群消息同步机器人

    原始网址:http://www.jianshu.com/p/7aeadca0c9bd# 最近 全栈数据工程师养成攻略 的微信群已经将近500人,开了二群之后为了打通不同微信群之间的消息,花了点时间做了 ...

  4. Android 性能优化(1)性能工具之「 lint 」 :Improving Your Code with lint:优化代码

    Improving Your Code with lint 1.See Also lint (reference) Using Android Annotations In addition to t ...

  5. 四种IO模型

    四种 IO 模型:       首先需要明确,IO发生在 用户进程 与 操作系统 之间.可以是客户端IO也可以是服务器端IO. 阻塞IO(blocking IO):     在linux中,默认情况下 ...

  6. C#模拟百度登录并到指定网站评论回帖(五)

    前面的四篇已经说完了全部的模拟百度登录,接下来就是到指定的网站去发表评论,当然你也可能是获取其他信息,其实这些套路都是万变不离其宗的,弄懂之后觉得像这种请求-响应获取信息的功能实在很简单(想起当初走的 ...

  7. Vue组件之间通信的三种方式

    最近在看梁颠编著的<Vue.js实战>一书,感觉颇有收获,特此记录一些比价实用的技巧. 组件是MVVM框架的核心设计思想,将各功能点组件化更利于我们在项目中复用,这类似于我们服务端面向对象 ...

  8. Objective-C设计模式——单例Singleton(对象创建)

    单例 和其它语言的单例产不多,可以说是最简单的一种设计模式了.但是有几个点需要注意下,单例就是一个类只有一个实例. 所以我们要想办法阻止该类产生别的实例,一般语言中都会将构造函数写为private.但 ...

  9. ubuntu下查看服务器的CPU详细情况

    https://www.cnblogs.com/liuq/p/5623565.html 全面了解 Linux 服务器 - 1. 查看 Linux 服务器的 CPU 详细情况 ubuntu下查看服务器的 ...

  10. 使用QTP录制自带Flight小实例

    1.双击打开QTP10.0,启动过程中测试类型选择“WEB”. 2.进入主界面,New——Test,新建一个测试用例. 3.点击Record按钮,Record and settings对话框中,可以选 ...