PAT_A1107#Social Clusters
Source:
Description:
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
Keys:
Code:
/*
time: 2019-06-23 14:07:12
problem: PAT_A1107#Social Clusters
AC: 34:25 题目大意:
把一群具有相同爱好的人归为一个社交圈,找出所有的社交圈
输入:
第一行给出,总人数N<=1e3,编号从1~N
接下来N行,给出第i个人的,爱好总数K,各个爱好
输出:
第一行给出,社交圈总数
第二行给出,各个社交圈的人数,从多到少 基本思路:
基于兴趣做并查集操作,
输入每个人的兴趣,首个兴趣的Hash值+1,标记人数
统计父结点个数及其孩子的哈希值即可
*/
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int M=1e3+;
int fa[M],man[M]={},ans[M]={}; int Father(int v)
{
int x=v,s;
while(fa[v] != v)
v = fa[v];
while(fa[x] != x){
s = fa[x];
fa[x] = v;
x = s;
}
return v;
} void Union(int v1, int v2)
{
int f1 = Father(v1);
int f2 = Father(v2);
fa[f2] = f1;
Father(v2);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE for(int i=; i<M; i++)
fa[i]=i; int n,m,h1,h2;
set<int> hobby,clster;
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d:%d", &m,&h1);
man[h1]++;
hobby.insert(h1);
for(int j=; j<m; j++)
{
scanf("%d", &h2);
hobby.insert(h2);
Union(h1,h2);
h1=h2;
}
}
for(auto it=hobby.begin(); it!=hobby.end(); it++){
ans[Father(*it)] += man[*it];
clster.insert(Father(*it));
}
printf("%d\n", clster.size());
sort(ans, ans+M, greater<int>() );
for(int i=; i<clster.size(); i++)
printf("%d%c", ans[i], i+==clster.size()?'\n':' '); return ;
}
PAT_A1107#Social Clusters的更多相关文章
- PAT1107:Social Clusters
1107. Social Clusters (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue When ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- 1107 Social Clusters[并查集][难]
1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...
- 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 分) 并查集模板
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
- 1107 Social Clusters——PAT甲级真题
1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...
- 1107. 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 ...
随机推荐
- HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- swift 2.0 语法 分支
import UIKit // 注意: Swift中可以不写;号, 但是有一定的前提条件, 一行只有一句代码 // 如果一行有多句代码, 那么;还是必须写 // 注意: Swift变态的地方 ...
- 在VM中安装Android4.4连接小米手机 之 安装小米手环APP
1.下载APP 在能够上网的情况的,搜索 小米手环APP就能够找到下载地址 2.安装APP 进入终端 3.在终端按下图红色区域语句依次输入. 先进入超级用户 找到下载的APP所在的路径 然后进入该路径 ...
- 6.6 random--伪随机数的生成
本模块提供了生成要求安全度不高的随机数.假设须要更高安全的随机数产生.须要使用os.urandom()或者SystmeRandom模块. random.seed(a=None, version=2) ...
- imagebutton 设置点击和按压效果
实现 点击 时 有按压效果 更换颜色 <ImageButton android:id="@+id/mediacontroller_Fullscreen_or_not" and ...
- 洛谷 P3806 点分治模板
题目:https://www.luogu.org/problemnew/show/P3806 就是点分治~ 每次暴力枚举询问即可,复杂度是 nmlogn: 注意 tmp[0]=1 ! 代码如下: #i ...
- nova service-list for juno kilo,liberty openstack
- 【转载】Java中Comparable和Comparator比较
[本文转自]http://www.cnblogs.com/skywang12345/p/3324788.html Comparable 简介 Comparable 是排序接口. 若一个类实现了Comp ...
- 9.15NOIP模拟题
GRYZ 模拟考试套题 9.15 gryz信息组专场 题目名称 最初的最初 太 妃 糖 可执行文件名 eat hwc dance sugar 输入文件 eat.in hwc.in dance.in s ...
- 安卓UI自适应性
出于安卓的碎片化原因,针对不同屏幕大小,最好是做到以下几点: 1.能用相对布局的就不用绝对布局,尽量使用权重,weight设置,相对布局和线性布局同条件情况下,优先选线性布局 2.在res目录下创建不 ...