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. Wireshark抓包保存文件(图片,视频,音频等)

    1.首先选择一个图片的分组 如图的9801 就是JPG 2.对下面的窗口里面选中JPEG File Interchange Format 右键选择 导出分组字节流 3.文件输入XXX.jpg,注意保存 ...

  2. i春秋第二届春秋欢乐赛RSA256writeup

    i春秋第二届春秋欢乐赛writeup 下载之后进行解压 发现四个文件 0x01看到题目是RSA的  又看到public.key 所以直接用kali linux的openssl 0x02可以看到e就是E ...

  3. [002] delete_duplication_of_linked_list

    [Description] Given a unsort linked list, delete all the duplication from them, no temporary space p ...

  4. Python排序算法之插入排序

    # 插入排序的工作原理是,对于每个未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.## 步骤:## 从第一个元素开始,该元素可以认为已经被排序# 取出下一个元素,在已经排序的元素序列中从后 ...

  5. UVA题解三

    UVA题解三 UVA 127 题目描述:\(52\)张扑克牌排成一列,如果一张牌的花色或者数字与左边第一列的最上面的牌相同,则将这张牌移到左边第一列的最上面,如果一张牌的花色或者数字与左边第三列的最上 ...

  6. Nginx源码分析--数组(转)

    原文地址:http://blog.csdn.net/marcky/article/details/5747431 备注:以下关于Nginx源码的分析基于淘宝开源项目Tengine. Nginx中对数组 ...

  7. Linux 不常用命令总结

    1. vim编辑模式下,搜索,/user,跳转下一个,小写的n 2.

  8. 数据分析python应用到的ggplot

    数据分析中应用到python中的ggplot库,可以用来画图 数据之类的用优达学院中课程七中的数据为例 数据是:https://s3.amazonaws.com/content.udacity-dat ...

  9. 关闭webstorm自动保存,并显示文件未保存标识

    1.取消自动保存 2.显示编辑状态设置:

  10. PHP获取以为数组中的最大值和最小值

    1.PHP获取一维数组中的最大值 <?php $a=array('1','3','55','99'); $pos = array_search(max($a), $a); echo $a[$po ...