1107 Social Clusters (30)(30 分)
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:
K~i~: h~i~[1] h~i~[2] ... h~i~[K~i~]
where K~i~ (>0) is the number of hobbies, and h~i~[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
并查集,凡是有相同的爱好的人都归到一个集合里,最后查一下每一个集合里的人数,存下来,降序排序输出。
代码:
#include <stdio.h>
#include <stdlib.h>
int n,m,d,c,f[],mp[][],num[],ans[];
void init() {
for(int i = ;i <= n;i ++) {
f[i] = i;
}
}
int getf(int x) {
if(x != f[x])f[x] = getf(f[x]);
return f[x];
}
void mer_(int x,int y) {
int xx = getf(x),yy = getf(y);
f[xx] = yy;
}
int cmp(const void *a,const void *b) {
return *(int *)b - *(int *)a;
}
int main() {
scanf("%d",&n);
init();
for(int i = ;i <= n;i ++) {
scanf("%d:",&m);
for(int j = ;j <= m;j ++) {
scanf("%d",&d);
mp[i][d] = ;
for(int k = ;k < i;k ++) {
if(mp[k][d])mer_(k,i);
}
}
}
for(int i = ;i <= n;i ++) {
int e = getf(i);
num[e] ++;
}
for(int i = ;i <= n;i ++) {
if(num[i])ans[c ++] = num[i];
}
qsort(ans,c,sizeof(int),cmp);
printf("%d\n",c);
for(int i = ;i < c;i ++) {
if(i)putchar(' ');
printf("%d",ans[i]);
}
}
1107 Social Clusters (30)(30 分)的更多相关文章
- 1107 Social Clusters (30 分)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 1107 Social Clusters (30 分)(并查集)
并查集的基本应用 #include<bits/stdc++.h> using namespace std; ; vector<int>vec[N]; int p[N]; con ...
- 1107 Social Clusters[并查集][难]
1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- 1107 Social Clusters——PAT甲级真题
1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...
- 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...
- 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
随机推荐
- Qt4.8.5配置相关问题
空余时间想看看Qt,在安装和编译过程中遇到了一些值得记录的东西,总结一下. (一)安装 1.先安装编译环境qt-creator-win-opensource-3.0.0.exe.使用默认路径C:\Qt ...
- vs2013数据库连接对应的dll
mysql for visual studio 1.1.1mysql connector net 6.3.9mysql connector/odbc 5.3
- Docker入门系列7 动态映射端口port mapping
为何想要动态映射端口呢? 因为刚开始run启动容器时,并不知道里面需要映射哪些端口,等容器已创建了,想映射端口. 当然可以通过先commit成镜像,然后再次run时指定端口,但会生成中间的镜像,对于有 ...
- WPF的ListView控件自定义布局用法实例
正文: 如何布局是在App.xaml中定义源码如下 <Application x:Class="CWebsSynAssistant.App" xmlns="ht ...
- Spring IOC源代码具体解释之容器依赖注入
Spring IOC源代码具体解释之容器依赖注入 上一篇博客中介绍了IOC容器的初始化.通过源代码分析大致了解了IOC容器初始化的一些知识.先简单回想下上篇的内容 加载bean定义文件的过程.这个过程 ...
- 大白第一章第四节dp例题
入口 UVALive - 3882 #include<cstdio> using namespace std; ; int n,m,k,f[N]; int main(){ //f[i]表示 ...
- the max number of open files 最大打开文件数 ulimit -n RabbitMQ调优
Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE) — RabbitMQ https://www.rabbitmq.com/i ...
- 如何利用Windows System Image Manager制作一个answer file
打开Windows System Image Manager 从菜单中创建一个新的Answer File 在窗体左下部的Windows Image处右键,选择菜单Select Windows Imag ...
- 流畅python学习笔记第十八章:使用asyncio包处理并发(一)
首先是线程与协程的对比.在文中作者通过一个实例分别采用线程实现和asynchio包实现来比较两者的差别.在多线程的样例中,会用到join的方法,下面来介绍下join方法的使用. 知识点一:当一个进程启 ...
- LeetCode 017 4Sum
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...