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 ...
随机推荐
- JS 的引用赋值与传值赋值
这个问题说大不大说小不小,如果你有幸踩了这个坑,一定会找这篇文章,哈哈~ 现说一下JS数字的类型:基本类型和引用类型 先看下下面两个栗子: var a = 30; var b = a; a = 20; ...
- scapy windows install
最近有点扫描网络的需求,都说scapy好,但是安装是个事(当然指的是windows安装)有个scapy3k,支持python3,可惜需要powershell,也就是说windows xp是没有戏了. ...
- 深入理解Java 8 Stream
Java 8中新增了Stream,主要是lambda表达式的应用,其链式调用简洁,用于高效表达集合操作. 先对Stream的使用做了解,参照blog. (1) 生成Stream的方式 主要有以下几种 ...
- 制作FAT12软盘以查看软盘的根目录条目+文件属性+文件内容
[-1]Before for specific info , please visit http://wiki.osdev.org/Loopback_Device [0]我们先上干货,看到效果后,我们 ...
- 有状态的EJB对象和无状态的EJB对象
一,定义有状态Bean和无状态Bean 有状态Bean: @Stateful @Remote public class StatefulEjbBean implements StatefulEjb{ ...
- EasyPlayerPro RTMP播放器助力远程娃娃机直播抓娃娃技术方案
远程娃娃机 目前市面上娃娃机的方案有很多种.核心的技术流程就是实现远程直播加上对娃娃机手臂的远程操作.其中最主要的技术还是视频直播方案,需要低延时,视频秒开等流媒体技术. 最简单的直播方案 视频直播方 ...
- JAVA解析XML之DOM方式
JAVA解析XML之DOM方式 准备工作 创建DocumentBuilderFactory对象; 创建DocumentBuilder对象; 通过DocumentBuilder对象的parse方法 ...
- Java 内存分配全面浅析(转)
本文将由浅入深详细介绍Java内存分配的原理,以帮助新手更轻松的学习Java.这类文章网上有很多,但大多比较零碎.本文从认知过程角度出发,将带给读者一个系统的介绍. 进入正题前首先要知道的是Java程 ...
- ArcGIS Overview Map(鹰眼/概览图)
一.说明 引用文件那块,可以参考我上一篇博文,arcgis api for javascript离线部署. 这篇博文中,地图占满整个body 二.运行效果 三.HTML代码 <!DOCTYPE ...
- java.sql.SQLException: Illegal connection port value '3306:success'
严重: Servlet.service() for servlet jsp threw exceptionjava.sql.SQLException: Illegal connection port ...