PAT甲级——A1107 Social Clusters
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:
Ki: hi[1] hi[2] ... hi[Ki]
where Ki (>) 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的更多相关文章
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
- pat甲级 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT A1107 Social Clusters (30 分)——并查集
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- A1107. Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT_A1107#Social Clusters
Source: PAT A1107 Social Clusters (30 分) Description: When register on a social network, you are alw ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 1107 Social Clusters——PAT甲级真题
1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 引入CSS样式表(书写位置)
CSS可以写到那个位置? 是不是一定写到html文件里面呢? 内部样式表 内嵌式是将CSS代码集中写在HTML文档的head头部标签中,并且用style标签定义,其基本语法格式如下: <head ...
- Vue实现一个学生信息录入系统,实现录入和删除
效果如下: 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- NX二次开发-读取图纸表格注释与部件属性关联的名字
NX11+VS2013 #include <uf.h> #include <uf_ui.h> #include <uf_tabnot.h> #include < ...
- NX二次开发-UFUN创建B面UF_MODL_create_bsurf
NX9+VS2012 #include <uf.h> #include <uf_modl.h> //创建一个B面 ;//U方向极数 ;//V方向极数 ;//U方向规则 ;//V ...
- NX二次开发-UFUN获取圆锥参数UF_MODL_ask_cone_parms
NX11+VS2013 #include <uf.h> #include <uf_modl.h> #include <uf_ui.h> UF_initialize( ...
- ionic-CSS:ionic select
ylbtech-ionic-CSS:ionic select 1.返回顶部 1. ionic select ionic select 的 select 相比原生的要更加美观些.但是弹出的可选选项样式是 ...
- C++ 编译过程简介
C/C++程序编译流程: 预处理->编译->汇编->链接 具体的就是: 源代码(source coprede)→预处理器(processor)→编译器(compiler)→汇编程序( ...
- 剑指offer——08斐波那契数列
题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 有多种方法,简单的循环.递归.动态规划: class Solutio ...
- 【HDUOJ】1213 How many tables
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意:Ignatius邀请了n个朋友来家里,朋友之间如果互相不认识的不想坐一起,所以至少要准备几 ...
- 【POJ】1321棋盘问题
题目链接:http://poj.org/problem?id=1321 题意:见题干,很清楚了. 题解:简单dfs,参照八皇后 代码: #include<iostream> #includ ...