[并查集] 1107. Social Clusters (30)
1107. Social Clusters (30)
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 (<=1000), 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 (>0) is the number of hobbies, and hi[j] 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 <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn=;
const int INF=1e9; int father[maxn];
int num[maxn];
int hobby[maxn]; void init()
{
for(int i=;i<maxn;i++)
{
father[i]=i;
num[i]=;
hobby[i]=-;
}
} int findFather(int x)
{
int a=x;
while(x!=father[x]) x=father[x];
while(a!=father[a])
{
int z=a;
a=father[a];
father[z]=x;
}
return x;
} int cnt; void uf(int a,int b)
{
int fa=findFather(a);
int fb=findFather(b);
if(fa!=fb)
{
father[fa]=fb;
num[fb]+=num[fa];
cnt--;
}
} int n; bool cmp(int a,int b)
{
return a>b;
} int main()
{
init();
cin>>n;
cnt=n;
for(int i=;i<=n;i++)
{
int k;
scanf("%d: ",&k);
for(int j=;j<k;j++)
{
int kj;
cin>>kj;
if(hobby[kj]==-)
{
hobby[kj]=i;
}
else
{
uf(hobby[kj],i);
}
}
}
cout<<cnt<<endl;
vector<int> ans;
for(int i=;i<=n;i++)
{
if(father[i]==i) ans.push_back(num[i]);
}
sort(ans.begin(),ans.end(),cmp);
for(int i=;i<ans.size();i++)
{
if(i>) cout<<" ";
cout<<ans[i];
}
return ;
}
[并查集] 1107. Social Clusters (30)的更多相关文章
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
- 1107 Social Clusters (30)(30 分)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 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 (Advanced Level) 1107. Social Clusters (30)
简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...
- 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 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[并查集][难]
1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...
随机推荐
- Linux-2.6_LCD驱动学习
内核自带的驱动LCD,drivers/video/Fbmem.c LCD驱动程序 假设app: open("/dev/fb0", ...) 主设备号: 29, 次设备号: 0--- ...
- python爬虫#网络请求requests库
中文文档 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html requests库 虽然Python的标准库中 urlli ...
- JavaWeb基础—JS学习小结
JavaScript是一种运行在浏览器中的解释型的编程语言 推荐:菜鸟教程一.简介js:javascript是基于对象[哪些基本对象呢]和和事件驱动[哪些主要事件呢]的语言,应用在客户端(注意与面向对 ...
- 2017-2018-1 20155320 课堂测试(ch06)
2017-2018-1 20155320 课堂测试(ch06) 1.(单选题 | 1 分) 下面代码中,对数组x填充后,采用直接映射高速缓存,所有对x和y引用的命中率为(D) A . 1 B . 1/ ...
- OpenCV学习系列(一) Mac下OpenCV + xcode人脸检测实现
# OpenCV学习系列(一) Mac下OpenCV + xcode人脸检测实现 [-= 博客目录 =-] 1-学习目标 1.1-本章介绍 1.2-实践内容 1.3-相关说明 2-学习过程 2.1-环 ...
- 22-[jQuery]-选择器, js jQuery对象转换
1.基础选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- JAVAWEB tomcat服务器启动错误原因总结
tomcat服务器启动错误: org.apache.catalina.LifecycleException 这种异常的原因是 servlet的代码出现了错误 实例: 这里的servlet由于使 ...
- 利用BlockingCollection实现生产者和消费者队列,实现写文本
最近开发几个小项目,需要把结果写到txt文件里面,并且按照时间进行分文件,由于对于效率要求较高,所以采用 生产者和消费者 模型来进行写出文本,线程中只需要添加队列就立即返回,而不需要等待写文件的时间 ...
- How to use the windows active directory to authenticate user via logon form 如何自定义权限系统,使用 active directory验证用户登录
https://www.devexpress.com/Support/Center/Question/Details/Q345615/how-to-use-the-windows-active-dir ...
- 详细介绍svn在eclipse中的使用(附图解说明)
SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subversion ...