Social Clusters

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

题意:
有N个人,每个人喜欢若干项活动,如果两个人有任意一项活动相同,那么就称他们处于同一个社交网络(若A和B属于同一个社交网络,B和C属于同一个社交网络,那么A、B、C属于同一个社交网络)。
求这N个人总共形成多少个社交网络。
参考代码:
 1 #include<cstdio>
2 #include<algorithm>
3 using namespace std;
4 const int N = 1010;
5 int father[N]; //存放父亲结点
6 int isRoot[N] = {0}; //记录每个结点是否作为某个集合的根结点
7 int course[N] = {0};
8 int findFather(int x){ //查找x所在集合的根结点
9 int a = x;
10 while(x!=father[x]){
11 x = father[x];
12 }
13 //路径压缩
14 while(a != father[a]){
15 int z = a;
16 a = father[a];
17 father[z] = x;
18 }
19 return x;
20 }
21
22 void Union(int a,int b){ //合并a和b所在的集合
23 int faA = findFather(a);
24 int faB = findFather(b);
25 if(faA != faB){
26 father[faA] = faB;
27 }
28 }
29 void init(int n){ //初始化father[i]为i,且flag[i]为false
30 for(int i=1;i<=n;i++){
31 father[i] = i;
32 isRoot[i] = false;
33 }
34 }
35
36 bool cmp(int a,int b){ //将isRoot数组从大到小排序
37 return a > b;
38 }
39
40 int main(){
41 int n,k,h;
42 scanf("%d",&n); //人数
43 init(n);
44 for(int i=1;i<=n;i++){ //对每个人
45 scanf("%d:",&k); //活动个数
46 for(int j=0;j<k;j++){ //对每个活动
47 scanf("%d",&h); //输入i号人喜欢的活动h
48 if(course[h] == 0){ //如果活动h第一次有人喜欢
49 course[h] = i; //令i喜欢活动h
50 }
51 Union(i,findFather(course[h])); //合并
52 }
53 }
54
55 for(int i=1;i<=n;i++){
56 isRoot[findFather(i)]++; //i的跟结点是findFzther(i),人数加1
57 }
58 int ans = 0; //记录集合数目
59 for(int i=1;i<=n;i++){
60 if(isRoot[i] != 0){
61 ans++; //只统计isRoot[i]不为0的
62 }
63 }
64 printf("%d\n",ans); //输出集合个数
65 sort(isRoot+1,isRoot+n+1,cmp);
66 for(int i=1;i<=ans;i++){ //依次输出每个集合内的人数
67 printf("%d",isRoot[i]);
68 if(i<ans)printf(" ");
69 }
70 return 0;
71 }


PAT A1107——并查集的更多相关文章

  1. PAT甲级 并查集 相关题_C++题解

    并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...

  2. PAT A1107 Social Clusters (30 分)——并查集

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

  3. 【algo&ds】【pat】5.并查集及其应用

    1.并查集的定义 在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集(Disjoint Sets)的合并及查询问题.有一个联合-查找算法(union-find algorithm)定义了两 ...

  4. PAT A 1118. Birds in Forest (25)【并查集】

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  5. PAT A1118 Birds in Forest (25 分)——并查集

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  6. PAT L2-013 红色警报(并查集求连通子图)

    战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不 ...

  7. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  8. PAT甲题题解-1034. Head of a Gang (30)-并查集

    给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...

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

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

随机推荐

  1. 深入理解JAVA虚拟机《二》

    对象.内存回收和垃圾收集算法 一.引用计数算法(不可靠) 现在很多比较普遍的判断对象是否存活的算法就是引用计数算法,其大概原理是:给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加1:当 ...

  2. uniapp小程序迁移到TS

    uniapp小程序迁移到TS 我一直在做的小程序就是 山科小站 也已经做了两年了,目前是用uniapp构建的,在这期间也重构好几次了,这次在鹅厂实习感觉受益良多,这又得来一次很大的重构,虽然小程序功能 ...

  3. Java标识符和关键字的区别!java基础 java必学

    任何计算机语言都离不开标识符和关键字,那我们就来简单讲一下他们两者的区别,希望有助于大家的的理解!本篇文章干货满满,如果你觉得难懂的话可以看下高淇老师讲的Java300集的教学视频,分选集,深度剖析了 ...

  4. 题解 CF468C Hack it!

    题目传送门 Description 设 \(f(i)\) 表示 \(i\) 的数码只和,给出 \(a\),求出 \(l,r\) 使得 \(\sum_{i=l}^{r} f(i)\equiv 0\pmo ...

  5. Oracle中常用的to_char用法详解

    Oracle函数to_char转化数字型指定小数点位数的用法 to_char,函数功能,就是将数值型或者日期型转化为字符型. 比如最简单的应用: -- 1.0123=>1.0123 SELECT ...

  6. VMware Tanzu社区版初体验

    VMware Tanzu社区版 VMware Tanzu Community Edition 是一个功能齐全.易于管理的 Kubernetes 平台,供学习者和用户使用. 它是一个免费的.社区支持的. ...

  7. Boost Started on Unix Variants

  8. git 更新与图形界面

    git 软件更新:git update-git-for-windows 或者 git update gitk 是一个历史记录的图形化查看器. 使用:只需 cd 到一个 Git 仓库,然后键入:gitk ...

  9. 对cpu与load的理解及线上问题处理思路

    cpu如何计算 当我们执行top命令的时候,看到里面的值(主要是cpu和load)值是一直在变的,因此有必要简单了解一下Linux系统中cpu的计算方式. cpu分为系统cpu和进程.线程cpu,系统 ...

  10. 你知道什么是JUC了吗?

    多线程一直Java开发中的难点,也是面试中的常客,趁着还有时间,打算巩固一下JUC方面知识,我想机会随处可见,但始终都是留给有准备的人的,希望我们都能加油!!! 沉下去,再浮上来,我想我们会变的不一样 ...