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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- shell 例子
shell编程入门 http://www.runoob.com/linux/linux-shell-variable.html http://c.biancheng.net/cpp/shell/ .查 ...
- __new__构造方法
""" 对象的创建过程:new创建 返回 模拟实例对象的创建过程. 为啥是静态方法? 先有new后来init.因为init是需要实例对象来调用的,需要一个实例对象和sel ...
- tp5 查询本年、本月、本周的方法
tp5自带了一些查询的方法,今天说一下查询本年.本月以及本周的方法 whereTime()//此方法代替了between and 方法 实际用法如下: ->whereTime('时间字段','y ...
- 单独编译和使用webrtc音频回声消除模块(附完整源码+测试音频文件)
单独编译和使用webrtc音频降噪模块(附完整源码+测试音频文件) 单独编译和使用webrtc音频增益模块(附完整源码+测试音频文件) 说实话很不想写这篇文章,因为这和我一贯推崇的最好全部编译并使用w ...
- javascript中内置函数
一.基本函数库 split():用于把一个字符串分割成字符串数组 toUpperCase(): substr(): 长度 length() 拼接(两种) + concat():合并多个字符串,并返回合 ...
- 玩转gulp之watch监听文件自动编译
博客移至 https://www.dodoblog.cn/blog?id=5befc928e0feb34495b57035 我们在写页面的时候,用到sass less等css预处理器的时候,虽然写的很 ...
- 一幅图解决R语言绘制图例的各种问题
一幅图解决R语言绘制图例的各种问题 用R语言画图的小伙伴们有木有这样的感受,"命令写的很完整,运行没有报错,可图例藏哪去了?""图画的很美,怎么总是图例不协调?" ...
- Cortex-M3的异常/中断屏蔽寄存器组
转自 1. Cortex-M3的异常/中断屏蔽寄存器组 注:只有在特权级下,才允许访问这3个寄存器. 名 字 功能描述 PRIMASK 只有单一比特的寄存器.置为1后,就关掉所有可屏蔽异常,只剩下NM ...
- 20130324 LBP CSLBP 全局存储区 局部存储区 char c[]=”hello world”和char *str=”hello world”的区别
1.LBP and CSLBP 2.再论char c[]=”hello world”和char *str=”hello world”的区别 /**************代码1************ ...
- 随笔-ansible-1
系统下所有的操作,从运维操作角度划分为两类: 1.文件传输 2.命令执行 系统下所有的操作,从自动化工作类型角度划分为: 1.应用部署 2.配置管理 3.任务流编排 使用root生成默认的秘钥对: # ...