本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731

1107 Social Clusters (30 分)
 

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:

K​i​​: h​i​​[1] h​i​​[2] ... h​i​​[K​i​​]

where K​i​​ (>) 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 (并查集)的更多相关文章

  1. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

  2. 1107 Social Clusters[并查集][难]

    1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...

  3. pat甲级 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  4. PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)

    题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...

  5. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

  6. PAT甲级——1114 Family Property (并查集)

    此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...

  7. PAT甲级——A1107 Social Clusters

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  8. [并查集] 1107. Social Clusters (30)

    1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...

  9. 1107 Social Clusters——PAT甲级真题

    1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...

随机推荐

  1. 吴恩达机器学习笔记(十一) —— Large Scale Machine Learning

    主要内容: 一.Batch gradient descent 二.Stochastic gradient descent 三.Mini-batch gradient descent 四.Online ...

  2. Spark- Linux下安装Spark

    Spark- Linux下安装Spark 前期部署 1.JDK安装,配置PATH 可以参考之前配置hadoop等配置 2.下载spark-1.6.1-bin-hadoop2.6.tgz,并上传到服务器 ...

  3. BZOJ 1198 [HNOI2006]军机调度:dfs

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1198 题意: 有n个雇佣军,m个任务. 第i个雇佣军能够参加cnt个任务,分别为temp[ ...

  4. Smarty模板重点汇总

    Smarty模板重点回顾:1.功能:前后端分离:2.实现方法:通过使用Smarty的核心类来实现,利用display方法来读取模板文 件,用正则进行替换,替换完保存到临时文件,再将临时文件加载到当前页 ...

  5. linkedhashSet和hashSet和TreeSet的区别(转)

    Set接口Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false.Set判断两个对象相同不是使用==运算符,而是根据equals方法.也就是说,只要两个对象用eq ...

  6. Codeforces 762C Two strings 字符串

    Cpdeforces 762C 题目大意: 给定两个字符串a,b\((len \leq 10^5)\),让你去b中的一个连续的字段,使剩余的b串中的拼接起来的两个串是a穿的子序列.最大化这个字串的长度 ...

  7. 数据库小记:根据指定名称查询数据库表名及根据指定名称查询数据库所有表中的字段名称(支持mysql/postgre)

    意:本篇文章仅适用于mysql和postgre这两种数据库 1.查询数据库中所有表名及对应表的详细信息 select * from INFORMATION_SCHEMA.tables 2.根据指定名称 ...

  8. The Review Plan I-禁位排列和容斥原理

    The Review Plan I Time Limit: 5000ms Case Time Limit: 5000ms Memory Limit: 65536KB   64-bit integer ...

  9. POJ3352(连通分量缩点)

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10352   Accepted: 514 ...

  10. Spring读取加密属性文件处理--待整理

    引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...