1107 Social Clusters——PAT甲级真题
1107 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:
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代表社交网络上的总人数,然后每个人的编号从1~N,接下来的N行,每行想给出一个数字K,接下来给出K个数h[i] ~h[k]代表每个人的爱好的编号
- 对于每一个测试数据先输出一共有多少个社交群体,然后按按照非递减序列输出每个社交群体一共有所少人数。ps: 有相同爱好的人在同一个社交群体
大致思路:
- 由题目可知,这道题让你对一组数据按照题目要求划分成不同的集合并统计每一个集合的人数,很明显,这道题要求我们用并查集去做
- 用一个数组fa[n]来表示每个集合的编号,用一个数组size用来记录每个集合的人数。初始时初始化为1~N,size初始化为1。当要进行合并操作实,size就用当前所在集合的人数加上合并集合的人数。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int fa[N], sizes[N];
int n;
int find(int x) {
if (x != fa[x]) fa[x] = find(fa[x]);
return fa[x];
}
int main() {
scanf("%d", &n);
for (int i = 0; i <= n + 1; i++) {
fa[i] = i; //先初始化
sizes[i] = 1;
}
vector<vector<int> > v(n + 1);
for (int i = 1; i <= n; i++) {
int k;
scanf("%d:", &k);
for (int j = 1; j <= k; j++) {
int x;
scanf("%d", &x);
v[i].push_back(x);
}
}
for (int i = 1; i <= n - 1; i++) {
for (int p = i + 1; p <= n; p++) {
if (find(p) == find(i)) continue;
for (int j = 0; j < v[i].size(); j++) {
if (find(v[p].begin(), v[p].end(), v[i][j]) != v[p].end()) {
sizes[find(p)] += sizes[find(i)];
fa[find(i)] = find(p);
break;
}
}
}
}
vector<int> ans;
for (int i = 1; i <= n; i++) {
if (find(i) == i) {
ans.push_back(sizes[i]);
// cnt++;
}
}
cout << ans.size() << endl;
sort(ans.begin(), ans.end());
for (int i = ans.size() - 1; i >= 0; i--) {
printf("%d", ans[i]);
if (i != 0)
cout << " ";
else
cout << endl;
}
return 0;
}
1107 Social Clusters——PAT甲级真题的更多相关文章
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
- PAT甲级真题及训练集
正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...
- PAT 甲级真题
1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...
- PAT甲级真题 A1025 PAT Ranking
题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...
- Count PAT's (25) PAT甲级真题
题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
随机推荐
- docker 安装 nexus3 初始密码不再是admin123
最近在docker上安装 nexus3 ,参照之前博客都提示 初始密码是admin/admin123 但是登录的时候出现如下提示: 很显然提示 admin用户的密码在/nexus-data/admi ...
- java--Aop--记录日志
package com.pt.modules.log; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; imp ...
- checkbox限制选中个数
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Kubernetes -- secret (敏感数据管理)
https://www.kubernetes.org.cn/secret secret 主要解决密码.token.密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者Pod Spec中 Se ...
- LInux-Lamp搭建
LInux-Lamp搭建 一 yum安装(自动会下载依赖包) (一)mysql安装 检测是否安装: yum list installed mysql* rpm -qa | grep mysql* 安装 ...
- @AliasFor 注解
Spring 框架提供了很丰富的注解可以让我们很方便的进行 Spring 配置,今天要讲的注解--@AliasFor之前你可能并没有关注过,因为平时开发时我们的确不太会用到. 我关注到这个注解是因为我 ...
- CF-1332 F. Independent Set
F. Independent Set 题意 一颗 n 个节点的树,求出每个\(edge-induced~subgraph\)的独立集个数之和. \(edge-induced~subgraph\)含义是 ...
- Codeforces Round #673 (Div. 2) A. Copy-paste(贪心)
题目链接:https://codeforces.com/contest/1417/problem/A 题意 给出一个大小为 $n$ 的数组 $a$,每次操作可以选择两个数,然后将一个数加到另一个数上, ...
- 1.ASP.NET Core 管道、中间件、依赖注入
自定义中间件(基于工厂) 自定义中间件(注入到第三方容器)
- CF1415-C. Bouncing Ball
CF1415-C. Bouncing Ball 题意: 在\(x\)轴上有\(n\)个点(从\(1\)到\(n\)),每个点都有一个值\(0\)或\(1\),\(0\)代表这个点不能走,\(1\)代表 ...