本文同步发布在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. javascript类的简单定义

    在面向对象编程中,类(class)是对象(object)的模板,定义了同一组对象(又称"实例")共有的属性和方法. Javascript语言不支持"类",但是可 ...

  2. html5--1.16 内联框架

    html5--1.16 内联框架 学习要点: 1.iframe内联框架2.综合实例1 1.iframe内联框架 1.iframe元素用来在文档中添加一个内联框架. 2.iframe为body元素的子元 ...

  3. int型变量,不使用中间变量完成互换

    package com.t_02; /** * 定义两个int类型的数,完成交换,不使用第三方变量 * @author Administrator * */ public class t1 { pub ...

  4. listen 70

    Better Sidewalks Could Bring Improved Public Health Most of our serious illnesses and deaths in the ...

  5. 线程绑定CPU核-sched_setaffinity

    CPU亲合力就是指在Linux系统中能够将一个或多个进程绑定到一个或多个处理器上运行. 一个进程的CPU亲合力掩码决定了该进程将在哪个或哪几个CPU上运行.在一个多处理器系统中,设置CPU亲合力的掩码 ...

  6. codeforces 569B B. Inventory(水题)

    题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. Python IOError: [Errno 13] Permission denied:

    一般是代码写错了,比如我遇到的问题就是由于 os.listdir() 传参传错导致的. 本应该传入字符串路径名,但传入了一个文件对象(object)

  8. Seal Report_20160923

    Seal Report算是报表工具中比较好用的一个,它提供了一个完整的从其他任何数据库产生报表的架构.该产品主要关注于容易安装和报表设计,一旦安装好,报表很快就可以建立并且发布.该组件完全开源,使用C ...

  9. BZOJ1146:[CTSC2008]网络管理

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  10. C语言单精度浮点型转换算法

    文章来源:http://blog.csdn.NET/educast/article/details/8522818 感谢原作者. 关于16进制浮点数对于大小为32-bit的浮点数(32-bit为单精度 ...