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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- Replication Controller、Replica Set
假如我们现在有一个Pod正在提供线上的服务,我们来想想一下我们可能会遇到的一些场景: 某次运营活动非常成功,网站访问量突然暴增 运行当前Pod的节点发生故障了,Pod不能正常提供服务了 第一种情况,可 ...
- leetcood学习笔记-67-二进制求和
题目描述: 第一次提交: class Solution: def addBinary(self, a: str, b: str) -> str: list_a,list_b=[],[] for ...
- springMVC快速入门 共分为五步
springMVC快速入门 共分为5步分别为: 1 导入依赖 2 spring-mvc.xml 配置 3 web.xml配置 4 自定义一个核心控制类 5 页面配置 详细步骤以及代码 ...
- android 插件化框架VitualAPK
推荐阅读: 滴滴Booster移动App质量优化框架-学习之旅 一 Android 模块Api化演练 不一样视角的Glide剖析(一) LeakCanary 与 鹅场Matrix ResourceCa ...
- sqoop简介和原理分析
Sqoop简介 Sqoop是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型数据库(例如 : MySQL , ...
- kafka拦截器原理|案例实操
拦截器原理 Producer拦截器(interceptor)是在Kafka 0.10版本被引入的,主要用于实现clients端的定制化控制逻辑. 对于producer而言,interceptor使得用 ...
- lua数据类型与变量
Lua数据类型与变量 Lua中有 8个基本类型分别为: nil.boolean.number.string.userdata.function.thread和 table. lua变量三种类型:全局 ...
- 执行SQL语句---INSERT/UPDATE/DELETE
1.执行SQL语句函数: int mysql_query(MYSQL* mysql, const char * query); query:所有的sql语句 2.例子: 向children表插入一条语 ...
- 分享一套高级Java笔试题(实拍高清图)
分享一套高级Java笔试题 微信群里群友分享的 刚好他在笔试 有些问题不会发到群里求助 如果你最近正好在面试 需要参考需要提升 这套试题或许对你有用 下面是部分分享原图 下面是微信群中群友的热议 非常 ...
- JQuery AJAX 通过一般处理程序 取列表
由于上一篇的积累 这一个就简单了 也就是把反回了字符串 显示到table中 $("#btnSearch").click(function () { $.post("Cur ...