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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 快速失败and安全失败
快速失败: 举个栗子: public static void main(String[] args) { ArrayList<String> list=new ArrayList<& ...
- wpf 解决 WPF SelectionChanged事件向上传递造成重复执行不想执行的函数的问题
例如 tabcontrol里有一个tabitem tabitem里有一个combox和一个datagrid tabcontrol combox datagrid都有SelectionChanged事件 ...
- python从字符串中提取数字_filter
my_str = '123and456' number = filter(str.isdigit, my_str ) # number = 123456 使用正则表达式: >>> i ...
- C++ 短信验证码/通知 - 代码示例
//接口类型:互亿无线触发短信接口,支持发送验证码短信.订单通知短信等. // ///////////////////账户注册:请通过该地址开通账户http://user.ihuyi.com/regi ...
- R语言 数据类型
R语言数据类型 通常,在使用任何编程语言进行编程时,您需要使用各种变量来存储各种信息. 变量只是保留值的存储位置. 这意味着,当你创建一个变量,你必须在内存中保留一些空间来存储它们. 您可能想存储各种 ...
- PHP ftp_set_option() 函数
定义和用法 ftp_set_option() 函数设置 FTP 连接的各种运行时选项. 如果成功,该函数返回 TRUE.如果失败,则返回 FALSE. 语法 ftp_set_option(ftp_co ...
- CommandLineToArgvW调EXE传入参数【转载】
#include <afxwin.h> // TODO: add your code here LPWSTR *szArglist = NULL; ; szArglist = Comma ...
- NX二次开发-UFUN工程图初始化视图信息UF_DRAW_initialize_view_info
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...
- ionic-CSS:ionic 表单和输入框
ylbtech-ionic-CSS:ionic 表单和输入框 1.返回顶部 1. ionic 表单和输入框 list 类同样可以用于 input 元素.item-input 和 item 类指定了文本 ...
- TVS(瞬态抑制二极管)、Schottky(肖特基二极管)、Zener (齐纳二极管,也称稳压二极管)主要特点及区别和使用
1. 简单介绍 TVS TVS(Transient Voltage Suppressor)二极管,又称为瞬态抑制二极管,是普遍使用的一种新型高效电路保护器件,它具有极快的响应时间(亚纳秒级)和相当高的 ...