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网络爬虫技术《一》 HttpClient

    HttpClient HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP ...

  2. ubuntu Nginx+tomcat 部署web项目

    最近学习了一下java web方面的知识,最后终于把项目部署到了阿里云服务器上,还是遇到了一些难点,记录总结一下 首先就是网上资料中,jdk都比较老了,最新的jdk14,没有了jre,这样导致了tom ...

  3. go语言游戏服务端开发(四)——RPC机制

    五邑隐侠,本名关健昌,12年游戏生涯. 本教程以Go语言为例. RPC指远程方法调用,游戏里引入RPC目的是降低跨进程交互的复杂度. 游戏业务设计为多go routine,一个玩家一个go routi ...

  4. 题解 P6454 麻将 加强版

    题目传送门 题目大意 不想写了,直接看题面吧. 思路 被这个题搞自闭了,因为读错题目想一个非常恶心的东西想了一场考试,然后就删代码,然后就被骂了.哎. 需要注意的是:我们只能选一次雀头 于是,不难看出 ...

  5. 无法获取指向控制台的文件描述符 (couldn't get a file descriptor referring to the console)

    背景 最近收拾东西,从一堆杂物里翻出来尘封四年多的树莓派 3B 主机来,打扫打扫灰尘,接上电源,居然还能通过之前设置好的 VNC 连上.欣慰之余,开始 clone 我的 git 项目,为它们拓展一个新 ...

  6. Scrum Meeting 0509

    零.说明 日期:2021-5-9 任务:简要汇报两日内已完成任务,计划后两日完成任务 一.进度情况 组员 负责 两日内已完成的任务 后两日计划完成的任务 qsy PM&前端 测试 测试 cyy ...

  7. [no code][scrum meeting] Alpha 2

    项目 内容 会议时间 2020-04-07 会议主题 功能规格说明书review 会议时长 30min 参会人员 OCR组(肖思炀,赵涛)和产品经理 $( "#cnblogs_post_bo ...

  8. JAVA实现表达式求导运算的分析总结

    1第一次作业 1.1题目描述 对形如4*x+x^2+x的多项式求导. 1.2类图 1.3度量分析 在完成第一次作业时,我的写法没有特别的"面向对象".唯一封装起来的是Node,代表 ...

  9. 2021.8.17考试总结[NOIP42]

    $\huge{取模不能比大小!}$ $\huge{取模不能比大小!}$ $\huge{取模不能比大小!}$ 有了打地鼠的前车之鉴,我深信树规板子是可以出现在联赛题里的. 所以T1十分钟码完直接溜了,后 ...

  10. stm32串口学习笔记

    stm32作为现在嵌入式物联网单片机行业中经常要用多的技术,相信大家都有所接触,今天这篇就给大家详细的分析下有关于stm32的出口,还不是很清楚的朋友要注意看看了哦,在最后还会为大家分享有些关于stm ...