PAT甲级1107. Social Clusters

题意:

当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友。一个“社会群体”是一群拥有一些共同兴趣的人。你应该找到所有的集群。

输入规格:

每个输入文件包含一个测试用例。对于每个测试用例,

第一行包含一个正整数N(<= 1000),一个社交网络中的总人数。因此,人数从1到N.然后N行跟随,每个给出一个人的爱好列表的格式:

Ki:hi [1] hi [2] ... hi [Ki]

其中Ki(> 0)是兴趣的数量,hi [j]是第j个兴趣的指数,

这是[1,1000]中的整数。

输出规格:

对于每种情况,在一行中打印网络中的集群总数。然后在第二行,以不增加的顺序打印群集中的人数。这些数字必须被一个空格分开,而行的末尾必须没有额外的空格。

思路:

并查集

ac代码:

C++

// pat1107.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
#include<unordered_set> using namespace std; const int maxn = 1002;
int n;
unordered_map<int, int> cluster;
int person_cnt[maxn];
int pre[maxn]; int find(int x)
{
return pre[x] == x ? x : find(pre[x]);
} void insert(int x, int y)
{
int xx = find(x);
int yy = find(y);
if (xx == yy) return;
pre[yy] = xx;
} bool cmp(const int a, const int b)
{
return a > b;
} int main()
{
scanf("%d", &n);
int num,ho,cnt = 1;
for (int i = 1; i < maxn; i++)
{
pre[i] = i;
} for (int i = 0; i < n; i++)
{
int where = -1;
scanf("%d:", &num);
vector<int> hobby(num);
for(int j = 0; j < num; j++)
{
scanf("%d", &hobby[j]);
if (cluster.find(hobby[j]) != cluster.end())
{
if(where == -1)
where = cluster[hobby[j]];
else
{
insert(where, cluster[hobby[j]]);
}
}
} if (where == -1)
{
for (int u = 0; u < num; u++)
cluster[hobby[u]] = cnt;
person_cnt[cnt]++;
cnt++;
}
else
{
for (int u = 0; u < num; u++)
cluster[hobby[u]] = where;
person_cnt[where]++;
}
} cnt--;
int res_cnt = 0;
vector<int> res;
for (int i = 1; i <= cnt; i++)
{
if(pre[i] != i)
person_cnt[find(i)] += person_cnt[i];
}
for (int i = 1; i <= cnt; i++)
{
if (pre[i] == i)
{
res_cnt++;
res.push_back(person_cnt[i]);
}
}
printf("%d\n", res_cnt);
sort(res.begin(), res.end(),cmp);
for (int i = 0; i < res_cnt - 1; i++)
printf("%d ", res[i]);
printf("%d\n", res[res_cnt - 1]);
return 0;
}

PAT甲级1107. Social Clusters的更多相关文章

  1. PAT甲级——1107 Social Clusters (并查集)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30  ...

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

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

  3. PAT甲级——A1107 Social Clusters

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

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

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

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

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

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

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

  7. pat甲级1107

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

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

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

  9. 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)

    题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...

随机推荐

  1. flask基础之jijia2模板使用基础(二)

    前言 在以前前后端不分离的时代,后台程序员往往又当爹又当妈,需要将前端程序员写的h5页面填充模板语言.而jijia2是一门十分强大的python的模板语言,是flask框架的核心模块之一.先简单介绍一 ...

  2. python之supervisor进程管理工具

    supervisor是python写的一个管理进程运行的工具,可以很方便的监听.启动.停止.重启一个或多个进程:有了supervisor后,就不用字节写启动和监听的shell脚本了,非常方便. sup ...

  3. SVM问题再理解与分析——我的角度

    SVM问题再理解与分析--我的角度 欢迎关注我的博客:http://www.cnblogs.com/xujianqing/ 支持向量机问题 问题先按照几何间隔最大化的原则引出他的问题为 上面的约束条件 ...

  4. Linux 编译 apr-util 时报错

    前言 Apache 2.4 以后的版本不再自带 APR 库(Apache Portable Runtime,Apache 可移植运行库),所以在安装 Apache 之前需要手动下载安装 APR 库. ...

  5. 在ubuntu上安装Chrome

    1.下载谷歌浏览器源文件.链接有很多,以下是64位版本的下载地址 https://dl.google.com/linux/direct/google-chrome-stable_current_amd ...

  6. 正排索引(forward index)与倒排索引(inverted index) (转)

    一.正排索引(前向索引) 正排索引也称为"前向索引".它是创建倒排索引的基础,具有以下字段. (1)LocalId字段(表中简称"Lid"):表示一个文档的局部 ...

  7. mysql 创建,授权,删除 用户

    1.创建用户 创建一个用户名是 lefunyun 密码是 X5A4FU8I0lKM21YPYUzP 账号 CREATE USER lefuyun@localhost IDENTIFIED BY 'X5 ...

  8. Linux命令参数处理 shell脚本函数getopts

    getopts 命令 用途 处理命令行参数,并校验有效选项. 语法 getopts 选项字符串 名称 [ 参数 ...] 描述 getopts 的设计目标是在循环中运行,每次执行循环,getopts ...

  9. [BZOJ4942][Noi2017]整数 线段树+压位

    用线段树来模拟加减法过程,维护连续一段中是否全为0/1. 因为数字很大,我们60位压一位来处理. #include<iostream> #include<cstring> #i ...

  10. day4正则表达式

    语法: 正则表达式是处理字符串的函数,我们在Excel函数中也有很多这样的公式,因为学过一些Excel,所以看一下有什么不同的方法. import re       #导入re模块,处理正则表达式的模 ...