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:

K​i​​: h​i​​[1] h​i​​[2] ... h​i​​[K​i​​]

where K​i​​ (>0) is the number of hobbies, and h​i​​[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 分)——并查集的更多相关文章

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

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

  2. 1053 Path of Equal Weight (30分)(并查集)

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

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

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

  4. [并查集] 1107. Social Clusters (30)

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

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

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

  6. 1107 Social Clusters (30)(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 (30)

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

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

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

  9. A1107. Social Clusters

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

随机推荐

  1. 【转】Git 教程之协同开发

    前面我们已经介绍过远程仓库的相关概念,不过那时并没有深入探讨,只是讲解了如何创建远程仓库以及推送最新工作成果到远程仓库,实际上远程仓库对于团队协同开发很重要,不仅仅是团队协同开发的基础,也是代码备份的 ...

  2. JS处理数组内如果相同ID追加一个属性(如字体颜色)

    var arr=[{id:0},{id:0},{id:3},{id:2},{id:0},{id:4},{id:0},{id:1},{id:1},{id:2},{id:2}]; for(var i=0; ...

  3. 通过AccessKey调用阿里云CDN接口刷新CDN资源案例

    通过AccessKey远程调用阿里云CDN接口,快速实现自动化集成部署. CdnService.java package com.nfky.cdn; import com.aliyuncs.Defau ...

  4. 初学HTML-9

    详情和概要标签:利用summary标签来描述概要信息,利用details标签来描述详情信息. 默认情况下是折叠显示. 格式:<details> <summary>概要信息< ...

  5. canvas-star6-drawMoon.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Java 初级软件工程师 认证考试试卷1

    Java 初级软件工程师 认证考试试卷   笔试(A卷)   考试时间150分钟 总分 100分     姓    名_______________________ 身份证号_____________ ...

  7. 【代码笔记】Web-HTML-标题

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  8. DHCP协议总结

    1.DHCP用于分配ip地址给主机. 2.DHCP报文也分为请求.应答. 3.DHCP请求报文,第一次是广播报文,因为还不知道DHCP server的MAC地址.后续续约的报文是单播发送.但是,到了7 ...

  9. 使用Twitter异常检测框架遇到的坑

    在Github上搜索“Anomaly Detection”,Twitter的异常检测框架(基于R语言)高居榜首,可见效果应该不错: 但是活跃度并不高,3-4年没人维护了: 因此在使用时难免会遇到一些坑 ...

  10. 《数据库系统概念》10-ER模型

    通过建立实体到概念模型的映射,Entity-Relationship Model可以表达整个数据库的逻辑结构,很多数据库产品都采用E-R模型来表达数据库设计. 一.E-R模型采用了三个基本概念:实体集 ...