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)的更多相关文章

  1. PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)

    题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...

  2. 1107 Social Clusters (30)(30 分)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  3. pat甲级 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  4. PAT (Advanced Level) 1107. Social Clusters (30)

    简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  5. 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)

    题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...

  6. 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  7. PAT甲级——1107 Social Clusters (并查集)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30  ...

  8. PAT-1107 Social Clusters (30 分) 并查集模板

    1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...

  9. 1107 Social Clusters[并查集][难]

    1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...

随机推荐

  1. Linux的信号解释

    转自:http://blog.csdn.net/yusiguyuan/article/details/43272225 整理后: 信号signal unix系统中,用信号实现软件中断 子进程结束-&g ...

  2. 如何保障Go语言基础代码质量?

    为什么要谈这个topic? 实践中,质量保障体系的建设,主要针对两个目标: 一是不断提高目标业务测试覆盖率,保障面向客户的产品质量:二就是尽可能的提高人效,增强迭代效率.而构建全链路质量卡点就是整个体 ...

  3. java rmi 入门实例

    java rmi 入门实例 (2009-06-16 16:07:55) 转载▼ 标签: java rmi 杂谈 分类: java-基础    java rmi即java远程接口调用,实现了2台虚拟机之 ...

  4. Linux5下安装MySQL过程记录

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页     回到顶级页面:PostgreSQL索引页 [作者 高健@博客园  luckyjackgao@gmail. ...

  5. python之打包、发布模块

    一.python中针对于写好的模块,并且比人也可以使用改模块,这样就可以以同意的打出来,让别人安装或者赋值过后可以更好的使用以及集成. 二.最近在学习python所以这里主要是记录一下python的打 ...

  6. SSIS 容器

    容器(Container)是控制流的特殊的任务(Task),它为一个或多个Task提供逻辑组合,可以实现工作流的重复执行和顺序执行,还可以把变量和事件处理程序的作用域缩小到容器中.不能在容器内的Tas ...

  7. jenkins maven设置settings.xml

    环境:jenkins.2.89.3 1.安装settings.xml管理插件Config File Provider Plugin  系统管理->管理插件->搜索Config File P ...

  8. 5种处理js跨域问题方法汇总

    1.JSONP跨域GET请求 ajax请求,dataType为jsonp.这种形式需要请求在服务端调整为返回callback([json-object])的形式.如果服务端返回的是普通json对象.那 ...

  9. Jmeter接口测试(七)用例数据分离

    之前我们的用例数据都是配置在 Jmeter Http 请求中,每次需要增加,修改用例都需要打开 jmeter 重新编辑,当用例越来越多的时候,用例维护起来就越来越麻烦,有没有好的方法来解决这种情况呢? ...

  10. kubenetes无法创建pod/创建RC时无法自动创建pod的问题

    一.问题概述 问题1: 虽然每次通过yaml创建rc都显示成功了,但是 kubectl get pod却没显示任何的pod. 问题2: 直接通过yaml创建pod提示apixxx 问题3: 通过.js ...