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. canvas-2lineJoin.html

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

  2. SQL Anywhere5.5: Metadata

    http://dcx.sybase.com/1101/en/dbprogramming_en11/ianywhere-data-sqlanywhere-saconnection-getschem633 ...

  3. Jetbrains IDE 中 compass sass 设置

    环境 Ubuntu 13.10 1. 安装ruby sudo apt-get install ruby 如果后面安装 compass 时遇到安装失败,提示:“[Ubuntu] ERROR: Faile ...

  4. JS中的三种循环

    三种循环1.while 2.do while 3.for 1.while: 语法结构:while(条件){代码块:改变条件} 步骤:1.初始化变量   2.判断条件  3.执行代码块  4.改变初始条 ...

  5. 网站pc端分享QQ好友,空间,微博

    在开发pc端网站的过程中,涉及到邀请好友的功能,之前单纯的复制粘贴已经无法满足用户的体验.故,仿照移动端添加自动分享到QQ好友,QQ空间,QQ微博的功能. 分享到QQ好友:http://connect ...

  6. JS列表

    promise 引用类型/值类型 ----- 对比python可变对象/不可变对象 原型继承

  7. 关于Natively Compiled Stored Procedures的优化

    Interpreted Transact-SQL stored procedures are compiled at first execution, in contrast to natively ...

  8. NPOI帮助类

    /// <summary> /// NPOI导出帮助类 /// </summary> public class NPOIHelper { /// <summary> ...

  9. sql 语句查询 sqlserver 数据库 MAC 地址

    declare @dbid int set @dbid=db_id('dbName') select distinct hostname, db_name(dbid), net_address, lo ...

  10. C#生成真值表

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...