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

题目大意:

  1. 给出一个正整数N代表社交网络上的总人数,然后每个人的编号从1~N,接下来的N行,每行想给出一个数字K,接下来给出K个数h[i] ~h[k]代表每个人的爱好的编号
  2. 对于每一个测试数据先输出一共有多少个社交群体,然后按按照非递减序列输出每个社交群体一共有所少人数。ps: 有相同爱好的人在同一个社交群体

大致思路:

  1. 由题目可知,这道题让你对一组数据按照题目要求划分成不同的集合并统计每一个集合的人数,很明显,这道题要求我们用并查集去做
  2. 用一个数组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甲级真题的更多相关文章

  1. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

  2. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  3. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

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

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

  5. PAT甲级真题及训练集

    正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...

  6. PAT 甲级真题

    1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...

  7. PAT甲级真题 A1025 PAT Ranking

    题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...

  8. Count PAT's (25) PAT甲级真题

    题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...

  9. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

随机推荐

  1. 深入Jar包:Gradle构建可执行jar包与访问jar包中文件夹与文件

    前言 Java的跨平台功能听起来很诱人可口,号称"Write Once,Run Everywhere",实际上是"Run Once,Debug Everywh" ...

  2. EXCEL序列

  3. 2020Nowcode多校 Round9 B.Groundhog and Apple Tree

    题意 给一棵树 初始\(hp=0\) 经过一条边会掉血\(w_{i}\) 第一次到达一个点可以回血\(a_{i}\) 在一个点休息\(1s\)可以回复\(1hp\) 血不能小于\(0\) 每条边最多经 ...

  4. AtCoder Beginner Contest 171

    比赛链接:https://atcoder.jp/contests/abc171/tasks A - αlphabet 题意 给出一个字母,如果为大写输出 'A',如果为小写输出 'a' . 代码 #i ...

  5. 洛谷 P6225 [eJOI2019]异或橙子 (树状数组)

    题意:有\(n\)个数,起始值均为\(0\),进行\(q\)次操作,每次输入三个数,如果第一个数为\(1\),则将第\(i\)个数修改为\(j\),如果为\(2\),则求区间\([l,r]\)内的所有 ...

  6. POJ - 2553 tarjan算法+缩点

    题意: 给你n个点,和m条单向边,问你有多少点满足(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}关系,并把这些点输出(要注意的是这个关系中是蕴含关系而不是且(&&)关系) 题解: ...

  7. Python3.5 配置MySql数据库连接

    #!coding:utf-8 import pymysql ''' Python3之后不再支持MySQLdb的方式进行访问mysql数据库: 可以采用pymysql的方式 连接方式: 1.导包 imp ...

  8. 【Azure Redis 缓存】使用Python代码获取Azure Redis的监控指标值 (含Powershell脚本方式)

    问题描述 通过Metrics监控页面,我们能得知当前资源(如Redis)的运行情况与各种指标.如果我们需要把指标下载到本地或者生成JSON数据导入到第三方的监控平台呢?Azure是否可以通过Pytho ...

  9. ubuntu 16.04 i386 安装 ruby + bundler + rails ; 搭建简单的网站bitbar

    参考 http://gorails.com/setup/ubuntu/16.04 概述 Project 2 主要探究对web的攻击,本次试验共有6个部分. Project 2中攻击的是一个提供电子货币 ...

  10. MyBatis中传参时为什么要用#{}

    MyBatis中传参时为什么要用#{},这个问题和MyBatis如何防止SQL注入类似.不过在解释这个问题之前,先解释一下什么是SQL注入,还有些称作注入攻击这个问题. SQL注入就是SQL 对传入参 ...