PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731
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 (≤), 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 (>) is the number of hobbies, and [ 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个人按照兴趣爱好分类,爱好有交集的归为一个社交团体,求社交团体的个数,然后将团体里面的人数进行降序输出。
思路:在并查集的操作上稍作修改,用根节点的值进行人数的统计,以每一行的第一个爱好代号作为当前数据的root,将之后的X与root进行union操作,若S[rootX]不等于root且 ≤ 0,意味着当前团体已经有了 | S[rootX] | 人,将S[rootX]的值加到S[root]上再进行合并。注意每一行开头的K:需要用字符串数组处理来获取K的值。
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#define MaxNum 1001
using namespace std;
vector <int> S(MaxNum, );
bool cmp(int a, int b) {
return a > b;
}
int getK(char *c);
void unionSet(int root, int X);
int find(int X);
int main()
{
int N, K;
scanf("%d", &N);
for (int i = ; i < N; i++) {
int root, X;
char c[];
scanf("%s%d", c, &X);
K = getK(c);
root = find(X);
S[root]--;//人数+1用负数表示
for (int j = ; j < K; j++) {
scanf("%d", &X);
unionSet(root, X);
}
}
vector <int> ans;
for (int i = ; i < MaxNum; i++) {
if (S[i] < )
ans.push_back(abs(S[i]));
}
sort(ans.begin(), ans.end(), cmp);
printf("%d\n", ans.size());
printf("%d", ans[]);
for (int i = ; i < ans.size(); i++)
printf(" %d", ans[i]);
printf("\n");
return ;
}
int getK(char *c) {
int sum = ;
for (int i = ; c[i] != ':'; i++)
sum = sum * + c[i] - '';
return sum;
}
void unionSet(int root, int X) {
int rootX = find(X);
if(S[rootX] <= && rootX != root){
S[root] += S[rootX];
S[rootX] = root;
}
}
int find(int X) {
if (S[X] <= )
return X;
else
return S[X] = find(S[X]);//递归地压缩路径
}
PAT甲级——1107 Social Clusters (并查集)的更多相关文章
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- 1107 Social Clusters[并查集][难]
1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...
- pat甲级 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- PAT甲级——1114 Family Property (并查集)
此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...
- PAT甲级——A1107 Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- 1107 Social Clusters——PAT甲级真题
1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...
随机推荐
- Hadoop- 分布式资源管理YARN架构讲解
YARN是分布式资源管理,每一台机器都要去管理该台计算机的资源,Yarn负责为MapReduce程序分配运算硬件资源.每一台机器的管理者叫 NodeManager,整个集群的管理者管理着整个集群的No ...
- Object.is() Pollyfill
if (!Object.is) { Object.is = function(x, y) { // SameValue algorithm if (x === y) { // Steps 1-5, 7 ...
- zepto.fullpage
内容来自:颜海镜 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Codeforces Gym 101190 NEERC 16 .L List of Primes(递归)
ls特别喜欢素数,他总是喜欢把素数集合的所有子集写下来,并按照一定的顺序和格式.对于每一个子集,集合内 的元素在写下来时是按照升序排序的,对于若干个集合,则以集合元素之和作为第一关键字,集合的字典序作 ...
- BZOJ_3671_[Noi2014]随机数生成器_set+贪心
BZOJ_3671_[Noi2014]随机数生成器_set Description Input 第1行包含5个整数,依次为 x_0,a,b,c,d ,描述小H采用的随机数生成算法所需的随机种子.第 ...
- poj3709 K-Anonymous Sequence[贪心+斜率优化dp]
地址 n个数,可进行把一个数减小的操作,代价为减小的值.现求使数列任意一个数都存在至少k-1个数和他相同,问操作的最小代价. 可以先考虑最小的数,由于只能减,所以必须得至少k-1个数减为最小数,贪心策 ...
- noip2013提高组:积木大赛
题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第n块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...
- puppet初始化安装和配置(puppet自动化系列1)
一.服务器规划 以下均直接yum安装最新版. 服务器操作系统为centos6.2 Puppetmaster1 10.168.32.116 puppstmaster1.jq.com Puppetmast ...
- request实现请求转发
ServletContext可以实现请求转发,request也可以. 在forward之前输入到response缓冲区中的数据,如果已经被发送到了客户端,forward将失败,抛出异常 在forwar ...
- 通过bed文件获取fasta序列
一.BED 文件格式 BED 文件格式提供了一种灵活的方式来定义的数据行,以用来描述注释的信息.BED行有3个必须的列和9个额外可选的列. 每行的数据格式要求一致. 必须包含的3列: 1.chrom, ...