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. oracle imp dmp命令

    vi par.txt userid=system/oracle tables=(user.table,...) query="where org_no like 32%" file ...

  2. ACM ICPC Kharagpur Regional 2017

    ACM ICPC Kharagpur Regional 2017 A - Science Fair 题目描述:给定一个有\(n\)个点,\(m\)条无向边的图,其中某两个点记为\(S, T\),另外标 ...

  3. 001使用smokeping监控idc机房网络质量情况

    最近工作比较忙,也没有时间写博客,看到好友芮峰云最近一直在写博客,所以也手痒了,就先把之前的一些积累下来的文章分享给大家. 本文是介绍如何的使用smokeping来监控idc机房的网络质量情况,从监控 ...

  4. 获取AD用户名

    var wshNetwork = new ActiveXObject("WScript.Network"); alert("域名 = "+ wshNetwork ...

  5. HDFS RAID实现方案(转)

    原文链接:http://blog.chinaunix.net/uid-20196318-id-3213700.html 分布式文件系统主要用于解决海量数据存储的问题,如Goolge.Facebook等 ...

  6. 响应式设计:根据不同设备引不同css样式

    <link rel="stylesheet" media="screen and (max-width:600px)" href="small. ...

  7. EasyUi – 5.修改$.messager.show() 弹出窗口在浏览器顶部中间出现

    由于在easyui中$.messager.show() 只有一种弹出方式(在浏览器的或下角弹出),我最近在做一个项目的时候需要在浏览器的顶部中间出现.由于自己写花那么多的时间,所以就去修改了原码(不推 ...

  8. jmeter-----如何安装插件管理?

    1.下载插件管理jar文件,http://www.jmeter-plugins.org/wiki/PluginsManager/ 2. 拷贝这jar文件到 \lib\ext文件夹里 3. 重新打开JM ...

  9. 翻译:MLAPP(2.1节 概率概述)

    笔者:尝试翻译MLAPP(Machine Learning: a Probabilistic Perspective)一书,供机器学习的学者参考,如有错误理解之处请指出,不胜感激!(如需转载,请联系本 ...

  10. 第 18 章 Django 入门

    当今的网站实际上都是富应用程序(rich application),就像成熟的桌面应用程序一样.Python提供了一组开发Web应用程序的卓越工具.在本章中,我们将学习如何使用Django(http: ...