[并查集] 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 ...
随机推荐
- 在jupyter中安装R的kernal
网上有安装完anaconda后可以直接使用conda 命令安装R的kernal,本人电脑上已经安装了anaconda和R,因此使用手动安装的方式安装. 安装环境: windows 8.1 企业版 An ...
- Keil C51编译报错error C141: syntax error
错误代码: typedef unsigned char uchar uchar KeyRowColumnScan() { GPIO_KEY = 0x0f; uchar key_value = ; // ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
- JavaScript 数组——filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()
filter(): 语法: var filteredArray = array.filter(callback[, thisObject]); 参数说明: callback: 要对每个数组元素执行 ...
- 洛谷P1313 计算系数【快速幂+dp】
P1313 计算系数 题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别 ...
- Unity3D — — Inspector面板编辑
转载官方文档,暂未深入研究 PropertyDrawer
- 前端常见算法面试题之 - 重建二叉树[JavaScript解法]
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列[1,2,4,7,3,5,6,8],和中序遍历序列[4,7 ...
- PHPCMS 栏目添加字段和修改描述textarea变成fceditor编辑器
一.添加字段方法: 1. 添加数据库字段:description1,添加位置:v9_catetory表 找到phpcms/moudles/admin/templates/category_add.tp ...
- 如何通过阿里云APP进行域名备案?阿里云备案流程需要多久?
如何通过阿里云APP进行域名备案? 1.准备备案材料(很多初次使用阿里云APP进行备案的同学会问备案需要准备哪些资料,不二版本下面就给大家一一列举出来) 个人备案需要材料: ⑴<用户网站备案授权 ...
- Cocos2dx源码赏析(2)之渲染
Cocos2dx源码赏析(2)之渲染 这篇,继续从源码的角度来跟踪下Cocos2dx引擎的渲染过程,以此来梳理下Cocos2dx引擎是如何将精灵等元素显示在屏幕上的. 从上一篇对Cocos2dx启动流 ...