PAT甲级1107. Social Clusters
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的更多相关文章
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
- pat甲级 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT甲级——A1107 Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 1107 Social Clusters——PAT甲级真题
1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- 1107 Social Clusters[并查集][难]
1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...
- pat甲级1107
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
- 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...
随机推荐
- ASLR
@author:dlive ASLR address space layout randomization 微软从windows vista/windows server 2008(kernel ve ...
- 64_s2
sipwitch-1.9.15-3.fc26.x86_64.rpm 13-Feb-2017 09:19 162822 sipwitch-cgi-1.9.15-3.fc26.x86_64.rpm 13- ...
- sicily 1017. Rate of Return
Description Jill has been investing in a mutual fund for a while. Since her income has varied, the a ...
- Minimum Palindromic Factorization(最少回文串分割)
Minimum Palindromic Factorization(最少回文串分割) 以下内容大部分(可以说除了关于回文树的部分)来自论文A Subquadratic Algorithm for Mi ...
- HDU 3533 Escape(BFS+预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...
- NIO-3网络通信
import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import ja ...
- AC日记——[USACO5.4]奶牛的电信Telecowmunication 洛谷 P1345
[USACO5.4]奶牛的电信Telecowmunication 思路: 水题: 代码: #include <cstdio> #include <cstring> #inclu ...
- ELK系列--问题汇总(二)
1.Kibana4 dashboard无法保存拖动的visualization位置 原因: 程序bug,json部分未能及时保存拖动的情况 解决方法: 手动在设置中,手动编辑dashboard的jso ...
- 【笔试题】Java 继承知识点检测
笔试题 Java 继承知识点检测 Question 1 Output of following Java Program? class Base { public void show() { Syst ...
- OOD沉思录 --- 面向动作与面向对象 --- 避免全能类
面向过程的软件开发通过非常集中化的控制机制来分解功能,在程序设计中表现就是大量的条件判断,深层次的循环嵌套等. 这种模式下,我们可以通过分析方法的参数,局部变量及其访问的全局变量来得到方法对数据的依赖 ...