PAT A1107 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 <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
using namespace std;
const int maxn = ;
int n;
int father[maxn]={};
int peo[maxn][maxn];
set<int> hob;
set<int> fah;
int res[maxn]={};
bool cmp(int a,int b){
return a>b;
}
void init(){
for(int i=;i<=maxn;i++){
father[i]=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;
}
void uni(int a,int b){
int fa=findfather(a);
int fb = findfather(b);
if(fa!=fb){
father[fb]=fa;
}
}
int main(){
init();
scanf("%d",&n);
for(int i=;i<=n;i++){
int k,fi;
scanf("%d: %d",&k,&fi);
peo[i][]=fi;
hob.insert(fi);
for(int j=;j<k;j++){
int x;
scanf("%d",&x);
peo[i][j]=x;
hob.insert(x);
uni(fi,x);
}
}
for(auto it:hob){
fah.insert(findfather(it));
//printf("%d %d\n",it,findfather(it));
}
printf("%d\n",fah.size());
for(int i=;i<=n;i++){
res[findfather(peo[i][])]++;
//printf("%d %d\n",findfather(peo[i][0]),res[findfather(peo[i][0])]);
}
sort(res,res+maxn,cmp);
for(int i=;i<fah.size();i++){
printf("%d",res[i]);
if(i<fah.size()-)printf(" ");
}
}
注意点:标准并查集,我是根据喜好来把人集合起来的,变量有点多,有点麻烦。看了大佬的代码,发现好像所有并查集的题目都是可以套模板的,他们是合并人,标记爱好,然后遍历isroot来得到集合个数
PAT A1107 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 ...
- 1053 Path of Equal Weight (30分)(并查集)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- 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> # ...
- A1107. Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find som ...
随机推荐
- Mybatis获取插入记录的自增长ID
转自:http://blog.csdn.net/tolcf/article/details/39035259 1.在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“k ...
- 【开发工具之Spring Tool Suite】6、用Spring Tool Suite简化你的开发
如果你是一个喜欢用spring的人,你可能会在欣赏spring的强大功能外,对其各样的配置比较郁闷,尤其是相差较大的版本在配置文件方面会存在差异,当然你可以去花不少的时间去网上查找相关的资料,当你准备 ...
- Archlinux/Manjaro使用笔记-安装配置搜狗输入法步骤
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一.安装qtwebkit-bin软件包解决qtwebkit无法编译安装问题 aurman -S qtwebkit-bin 二.安 ...
- ORM&MySQL
概念: ORM:对象关系映射 , 全拼 Object-Relation Mapping ,是一种为了解决面向对象与关系数据库存在的互不匹配现象的技术.主要实现模型对象到关系型数据库数据的映射.比如:把 ...
- 使用volley上传多张图片,一个参数对应多张图片,转载
https://my.oschina.net/u/1177694/blog/491834 原帖地址 而如果使用volley的话,因为请求数据那些都很简便,但遇到上传文件就麻烦那可不好,同时使用多个网络 ...
- Java数据解析---PULL
安卓和JAVA解析xml文件的三种方式: 1.PULL解析 2.SAX解析 3.DOM解析 三者各有所长,依情况选择解析方式 1.PULL和SAX均采用流式解析,意味着只能从头读到底,无法像DOM解析 ...
- python的subprocess模块执行shell命令
subprocess模块可以允许我们执行shell命令 一般来说,使用run()方法就可以满足大部分情况 使用run执行shell命令 In [5]: subprocess.run('echo &qu ...
- 洗礼灵魂,修炼python(10)--有趣的判断分支+从实例中掌握循环语句
所有的编程语言里都有判断语句和循环语句. 判断语句则是用来分支程序流程的 循环语句则是为了实现一个效果,让程序的规律性的重复操作 不用说,分支和循环自然在python里也是有的 一,条件判断:if,i ...
- apache 80端口占用问题
今天安装mongodb后发现apache无法启动 命令行 services.msc 打开服务 在服务里启动Apache2a服务,报错误码1 网上查有很多情况都报的1 可以通过命令行下 执行apach ...
- P进制转Q进制
// 对一个P进制的数,如果要转换成Q进制的数 // 1)将P进制数x转换成十进制数y int y=0,product=1;//product在循环中会不断成P,得到1.P^2..... while( ...