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 ...
随机推荐
- Cisco的互联网络操作系统IOS和安全设备管理器SDM__路由器软、硬件知识
路由器软.硬件知识 1.路由器的组件: 组件 解释 Bootstrap 存储在ROM中的微代码,bootstrap用于在初始化阶段启动路由器.它将启动路由器然后装入IOS POST(开机自检) 存储在 ...
- hdu3635 Dragon Balls
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- Atcoder ABC162 D - RGB Triplets
传送门:D - RGB Triplets 题意:给你一个只含'R','G','B'的字符串,求有多少个长度为3且每个字符都不相等,并且第一第二和第二第三的区间长度不同的子序列. 题解:统计每个字符各 ...
- Codeforces Round #613 (Div. 2) B. Just Eat It! (DP)
题意:有一个长度为\(n\)的序列,找出最大的长度不为\(n\)的子段和,问最大子段和是否小于所有元素和. 题解:最大子段和我们可以直接用dp来找,每次状态转移为:\(dp[i]=max(dp[i-1 ...
- 牛客小白月赛28 D.位运算之谜 (位运算)
题意:给你两个正整数\(x\)和\(y\),求两个正整数\(a\),\(b\),使得\(a+b=x\),\(a\)&\(b\)=\(y\),如果\(a\),\(b\),输出\(a\ xor \ ...
- 错误: 未能完成程序集的安装(hr = 0x8007000b)。探测终止。
解决方案:VS中"工具"->"选项"->"Web项目"->"对网站和项目使用IIS Express的64位版& ...
- C# List.Sort与IComparer接口及Comparison委托应用于集合排序
C#里List.Sort的用法 using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- 主动降噪,通话降噪及AI降噪之辨
近日,三星发布的Buds Pro 耳机中,宣传有以下功能.其中涉及到噪声的,有主动降噪,通话降噪及智能降噪,很多人对他们的具体用途容易混淆,今天我们来辨析一下. 主动降噪和通话降噪完全不是一个概念,无 ...
- python之字符串strip、rstrip、lstrip的方法
1.描述 strip():用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列 rstrip():用于移除字符串右边指定的字符(默认为空格或换行符)或字符序列 lstrip():用于移除字符串 ...
- 牛客网多校第5场 F take 【思维+数学期望】
题目:戳这里 思路来源:视频讲解 题意:有n个箱子按1...n标号,每个箱子有大小为di的钻石概率为pi,我们初始有个大小为0的钻石,从1到n按顺序打开箱子,遇到比手中大的箱子就换,求交换次数的数学期 ...