PAT A1107——并查集
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
题意:
有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——并查集的更多相关文章
- PAT甲级 并查集 相关题_C++题解
并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...
- PAT A1107 Social Clusters (30 分)——并查集
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 【algo&ds】【pat】5.并查集及其应用
1.并查集的定义 在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集(Disjoint Sets)的合并及查询问题.有一个联合-查找算法(union-find algorithm)定义了两 ...
- PAT A 1118. Birds in Forest (25)【并查集】
并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...
- PAT A1118 Birds in Forest (25 分)——并查集
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- PAT L2-013 红色警报(并查集求连通子图)
战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不 ...
- PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- PAT甲题题解-1034. Head of a Gang (30)-并查集
给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
随机推荐
- docker - compose 部署 Nginx
主要介绍 docker 中 Nginx 的部署及项目目录挂载券的方法.docker 中部署一个服务,有三种方法,分别是 docker run.Dockerfile.docker-compose . 下 ...
- node-pre-gyp以及node-gyp的源码简单解析(以安装sqlite3为例)
title: node-pre-gyp以及node-gyp的源码简单解析(以安装sqlite3为例) date: 2020-11-27 tags: node native sqlite3 前言 简单来 ...
- 利用caffe.proto生成caffe.pb.h
完全按照博文来就好了:http://blog.csdn.net/u012905422/article/details/52794693
- Serverless 工程实践 | 零基础上手 Knative 应用
作者|刘宇 前言:Knative 是一款基于 Kubernetes 的 Serverless 框架.其目标是制定云原生.跨平台的 Serverless 编排标准. Knative 介绍 Knative ...
- k8s调度器介绍(调度框架版本)
从一个pod的创建开始 由kubectl解析创建pod的yaml,发送创建pod请求到APIServer. APIServer首先做权限认证,然后检查信息并把数据存储到ETCD里,创建deployme ...
- java课堂测试3第一部分(未完善)
package test3;import java.util.*; public class Grade2 { static String[][] mis=new String[500][4]; // ...
- 结对编程——带UI的小初高数学学习软件
一.简介 本次项目要求: 1.所有功能通过图形化界面操作,可以是桌面应用,可以是网站(编程语言和技术不限): 2.用户注册功能.用户提供手机号码,点击注册将收到一个注册码,用户可使用该注册码完成注册: ...
- NX 图标
vector_on_curve crosscut_zig_zag_with_lifts vector_along_curve zlevel_zig add_new_sc zlevel_zi ...
- 【数据结构】<栈的应用>回文判断
通过栈与队列相关内容的学习,我们知道,栈是"先进后出"的线性表,而队列是"先进先出"的线性表.可以通过构造栈与队列来实现在这一算法.将要判断的字符序列依次压栈和 ...
- [对对子队]会议记录4.17(Scrum Meeting8)
今天已完成的工作 何瑞 工作内容:修复了一些bug,优化了UI 相关issue:搭建关卡1 相关签入:4.17签入1 吴昭邦 工作内容:做了一些流水线系统的错误处理,添加了合成失败了之 ...