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第一次出现的人 ...
随机推荐
- SpringBoot 如何进行限流?老鸟们都这么玩的!
大家好,我是飘渺.SpringBoot老鸟系列的文章已经写了四篇,每篇的阅读反响都还不错,那今天继续给大家带来老鸟系列的第五篇,来聊聊在SpringBoot项目中如何对接口进行限流,有哪些常见的限流算 ...
- Geostatistical Analyst Tools(Geostatistical Analyst 工具)
Geostatistical Analyst 工具 1.使用地统计图层 # Process: GA 图层至格网 arcpy.GALayerToGrid_ga("", 输出表面栅格, ...
- linux启动redis命令
首先进入到/usr/local/bin目录下(因为你redis安装的目录绝大多数都在这里) root@xxxx:/usr/local/bin#:redis-server wangconfig/redi ...
- 题解 CF1119H Tripe题解
题目传送门 题目大意 给出\(n,t,x,y,z\),值域\(\le 2^t\),给出\(n\)个三元组\((a_i,b_i,c_i)\),表示有\(x\)个\(a_i\),\(y\)个\(b_i\) ...
- API代码实战
API实例一: login.py文件 #!/usr/bin/env python #!coding:utf-8 from flask import Flask,jsonify from flask_r ...
- mybatis学习笔记(2)基本原理
引言在mybatis的基础知识中我们已经可以对mybatis的工作方式窥斑见豹(参考:<MyBatis----基础知识>).但是,为什么还要要学习mybatis的工作原理?因为,随着myb ...
- Scrum Meeting 0501
零.说明 日期:2021-5-1 任务:简要汇报两日内已完成任务,计划后两日完成任务 一.进度情况 组员 负责 两日内已完成的任务 后两日计划完成的任务 qsy PM&前端 整装待发,准备冲刺 ...
- 【二食堂】Beta - Scrum Meeting 10
Scrum Meeting 10 例会时间:5.25 18:30~18:50 进度情况 组员 当前进度 今日任务 李健 1. 继续文本导入.保存部分的工作issue 2. 完成了技术博客 1. 继续文 ...
- Seata的一些概念
Seata的一些概念 一.什么是seata 二.AT模式的介绍 1.前提条件 2.整体机制 3.读写隔离的实现 1.写隔离 2.读隔离 三.事务分组 1.事务分组是什么? 2.通过事务分组如何找到后端 ...
- FreeRTOS学习笔记——FreeRTOS 任务基础知识
RTOS 系统的核心就是任务管理,FreeRTOS 也不例外,而且大多数学习RTOS 系统的工程师或者学生主要就是为了使用RTOS 的多任务处理功能,初步上手RTOS 系统首先必须掌握的也是任务的创建 ...