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 ...
随机推荐
- Linux Sed命令具体解释+怎样替换换行符"\n"(非常多面试问道)
Sed Sed是一个强大的文本处理工具 能够採用正则匹配.对文本进行插入删除改动等操作 Sed处理的时候,一次处理一行,每一次把当前处理的存放在暂时缓冲区.处理完后输出缓冲区内容到屏幕,然后把下一行读 ...
- hibernate出现 org.hibernate.PropertyNotFoundException: field [departmen] not found on cn.itcast.hibernate.domain.Employee1错误
hibernate出现 org.hibernate.PropertyNotFoundException: field [departmen] not found on cn.itcast.hibern ...
- 小练习:用socket实现Linux和Windows之间的通信
在日常生活中,绝大部分人使用的机器通常是windows系统,可是对于研发人员,开发.编译等工作往往是建立在linux机器上.其实.在服务器方面,Linux.UNIX和WindowsServer占领了市 ...
- 开源项目WebImageView载入图片
项目地址:https://github.com/ZaBlanc/WebImageView 作者对载入图片,以及图片的内存缓存和磁盘缓存做了封装. 代码量不多.可是可以满足一般的载入图片. 先看下项目结 ...
- ssh无密码登陆权威指南
[0]写在前面 由于ssh 实现的是免密码登陆,大致步骤是: 0.1) client通过ssh登陆到server: 0.2) server检查家目录下的.ssh文件, 并发送公钥文件 authoriz ...
- 多媒体开发之sps---解析sps得到图像的宽高
(1)通过块的宽高解析出真个h264的分辨率 如何解析SDP中包含的H.264的SPS和PPS串 http://www.pernet.tv.sixxs.org/thread-109-1-1.html ...
- hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩
Description Recognizing junk mails is a tough task. The method used here consists of two steps: 1) ...
- WPF中的ListBox实现按块显示元素的方法
本文实例讲述了WPF中的ListBox实现按块显示元素的方法.分享给大家供大家参考,具体如下: 注意:需要设置ListBox的属性 ScrollViewer.HorizontalScrollBarVi ...
- EasyNVR NVR网页无插件直播在兼容宇视NVR RTSP流媒体时PLAY过程对Scale的兼容
前一段在维护EasyNVR客户的过程中遇到一个问题,在接入宇视NVR的时候,就是明明在vlc中能非常正常播放的视频流,却用EasyRTSPClient RTSP客户端拉流的协议交互过程中,PLAY命令 ...
- 我的Android进阶之旅------>Android通过使用Matrix旋转图片来模拟碟片加载过程
今天实现了一个模拟碟片加载过程的小demo,在此展示一下.由于在公司,不好截取动态图片,因此就在这截取两张静态图片看看效果先. 下面简单的将代码列出来. setp1.准备两张用于旋转的图片,如下:lo ...