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. 0621补-MVC的基础整理

    包括:Model-模型.view-视图.Controller-控制器. 特点: 将功能强制分成两个部分,显示html文件,和逻辑PHP文件: 要求浏览器请求负责功能的PHP逻辑文件,该PHP逻辑文件, ...

  2. vs2017 + miniUI + dapper 添加、修改、删除、查询操作

    A.数据库表引用先前建立的company 公司信息表. B.建立文件: views > Home > Company.cshtml(新建文件) ,代码如下(直接复制即可) @{ Layou ...

  3. vue.js学习文档

    1.实例化vue对象 new Vue(){ } 2.对象属性 el: 控制的属性 data: 数据存储位置 methods: 方法存储位置 template: 模板样式 computed: 计算属性 ...

  4. 51nod1459 迷宫游戏

    1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你 ...

  5. EditText(7)EditText输入事件监听

    EditText.addTextChangedListener(TextWatcher watcher); void initSearch(){ search = (EditText) findVie ...

  6. SQL在一张表中根据父ID获取所有的子ID

    with a as ( select id,name,parentid from categories where id=53 union all select x.id,x.name,x.paren ...

  7. 从 C++ 到 Objective-C 的快速指南

    简介 当我开始为iOS写代码的时候,我意识到,作为一个C++开发者,我必须花费更多的时间来弄清楚Objective-C中怪异的东西.这就是一个帮助C++专家的快速指南,能够使他们快速的掌握Apple的 ...

  8. 由ibatis向mybatis的转变

    我将项目引用的ibatis换成mybatis 过程中遇到一个问题:org.apache.ibatis.datasource.DataSourceException: Unknown DataSourc ...

  9. 安装linux mint 18.3 后要做的

    使用u盘安装的linux mint 18.3,安装过程基本顺利 发现在安装过程中使用中文语言的话会使得下载附加软件的速度快很多 安装完成之后要做的事情有: 1.字体 默认的楷体字比较难看,在软件管理器 ...

  10. SQLState: 23000

    今天登陆项目的时候,报500,日志显示如下: 解决办法是: 首先,删除序列:DROP SEQUENCE sys_log_seq 然后,新建序列:CREATE SEQUENCE sys_log_seq ...