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 ...
随机推荐
- 模拟IE各种版本的方法
下载360极速浏览器.开启“兼容模式” 默认会是IE7.可以通过控制台(Ctrl + shift + I)调整各个版本
- c# .net 我的Application_Error 全局异常抓取处理
protected void Application_Error(object sender, EventArgs e) { //在出现未处理的错误时运行的代码 ...
- JavaScript -- 没事看看
@.delete 原文:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/delete
- scrollview gridview
package com.fangdamai.salewinner.ui.customer; import android.content.Context;import android.content. ...
- Drupal 主题的表现形式
1.template.php /** * Implements hook_theme(). */ function yourtheme_theme($existing, $type, $theme ...
- 首选项框架PreferenceFragment部分源代码分析
由于要改一些settings里面的bug以及之前在里面有做过勿扰模式,准备对勿扰模式做一个总结,那先分析一下settings的源代码,里面的核心应该就是android3.0 上面的首选项框架Prefe ...
- 说明sizeof和strlen之间的区别。
解析:由以下几个例子我们说明sizeof和strlen之间的区别.第1个例子: sizeof(ss)结果为4,ss是指向字符串常量的字符指针.sizeof(*ss)结果为1,*ss是第一个字符.第2个 ...
- 连通性 保证f具有介值性质
- HNOI2017
本蒟蒻表示终于把$HNOI2017$全AC了... 万岁! 附上各个题的题解: $DAY1$: $T1$: BZOJ4825: [Hnoi2017]单旋 $T2$: BZOJ4826: [Hnoi20 ...
- hdu5325 树的思维题
pid=5325">http://acm.hdu.edu.cn/showproblem.php? pid=5325 Problem Description Bobo has a tre ...