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. VB6 加载水晶报表例子

    VB6 加载水晶报表例子 先按照水晶报表组件 Crystal Reports,Business Objects,现已被SAP收购. 再添加引用 'Library: CRAXDRT 'C:\Progra ...

  2. 20155202 2016-2017-2 《Java程序设计》第9周学习总结

    20155202 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 整合数据库: JDBC驱动程序,按照方式分4种类型: Type1 : JDBC-ODBC B ...

  3. stat命令的实现-mysate 20155239吕宇轩

    stat命令的实现-mysate 20155239吕宇轩 学习使用stat(1),并用C语言实现 提交学习stat(1)的截图 man -k ,grep -r的使用 伪代码 产品代码 mystate. ...

  4. 13-[CSS]-postion位置:相relative,绝absolute,固fixed,static(默认),z-index

    1.postion位置属性 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  5. 2-[HTML]--介绍

    1.HTML简介 HTML,全称是超文本标记语言(HyperText Markup Language),它是一种用于创建网页的标记语言.标记语言是一种将文本(Text)以及文本相关的其他信息结合起来, ...

  6. 微信小程序:选项卡页面切换

    一.功能描述 在同一个页面内实现不同展示页面的切换功能,如下图所示 二.代码实现 1. index.js Page({ /** * 页面的初始数据 */ data: { currentData : 0 ...

  7. Dbzoj#3188. [Coci 2011]Upit

    写道数据结构练练手哈哈哈 // It is made by XZZ #include<cstdio> #include<algorithm> #include<cstdl ...

  8. vmware打开vmx文件不能创建虚拟机的问题

    这种情况一般结束 vmware-tray 进程,然后直接在文件管理器里打开vmx文件即可创建(打开方式为vmware), 直接在vmware里打开虚拟机文件可能会出现无反应(不创建虚拟机)的情况.

  9. Design3:数据层次结构建模之二

    SQL Server提供了一个新的数据类型 HierarchyID,用来处理层次结构的数据,这个数据类型是系统内置的CLR数据类型,不需要专门激活 SQL/CLR 功能即可使用.当需要表示各值之间的嵌 ...

  10. 大话 .Net 之内存管理

    在一次偶然的机会中,我来到了恒生的大家庭.又在一次偶然的机会中,我很荣幸的被勇哥信任并让我写一篇季刊的文章.可能人生之中充满了无数次的偶然机会,我们只有抓住眼前的“偶然”,才可以创建人生.当我接到这个 ...