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 分)的更多相关文章

  1. 1107 Social Clusters (30 分)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  2. 1107 Social Clusters (30 分)(并查集)

    并查集的基本应用 #include<bits/stdc++.h> using namespace std; ; vector<int>vec[N]; int p[N]; con ...

  3. 1107 Social Clusters[并查集][难]

    1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...

  4. [并查集] 1107. Social Clusters (30)

    1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...

  5. PAT甲级——1107 Social Clusters (并查集)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30  ...

  6. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

  7. 1107 Social Clusters——PAT甲级真题

    1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...

  8. 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)

    题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...

  9. 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

随机推荐

  1. mysql索引类型normal,unique,full text

    normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用作索引时,可设置为unique full textl: 表示 全文搜索的索引. FULL ...

  2. ip获取位置

    $ip = $_SERVER["REMOTE_ADDR"]; $url = "http://ip.taobao.com/service/getIpInfo.php?ip= ...

  3. onvif 开发之video streamer---onvif实现功能和经验

    目录(?)[-] 一产生onvif源码框架 从wsdl生成C头文件 从头文件生成源码框架 二创建soap运行环境 三RTSP视频对接 实现GetCapabilities命令 实现GetServices ...

  4. C# Array类的浅复制Clone()与Copy()的差别

    1 Array.Clone方法 命名空间:System 程序集:mscorlib 语法: public Object Clone() Array的浅表副本仅复制Array的元素,不管他们是引用类型还是 ...

  5. SQL Server 存储过程的几种常见写法分析,我们该用那种写法

    本文出处: http://www.cnblogs.com/wy123/p/5958047.html 最近发现还有不少做开发的小伙伴,在写存储过程的时候,在参考已有的不同的写法时,往往很迷茫,不知道各种 ...

  6. 苹果开发之COCOA编程(第三版)下半部分

    第十八章:Image和鼠标事件 1.NSResponderNSView继承自NSResponder类.所有的事件处理方法都定义在NSResponder类中.NSResponder申明了如下方法:- ( ...

  7. Mac 常用属性

    如果需要让隐藏的文件可见. 具体做法就是打开一个Terminal终端窗口,输入以下命令: 对于OS X Mavericks 10.9: defaults write com.apple.finder ...

  8. 九度OJ 1156:谁是你的潜在朋友 (并查集)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5802 解决:2593 题目描述: "臭味相投"--这是我们描述朋友时喜欢用的词汇.两个人是朋友通常意味着他们存在着许多 ...

  9. sed命令使用举例

    选择操作的行范围 sed -n '1,2p' testsed2.txt  匹配第1到2行 sed -n '/a/,/b/p' testsed2.txt  匹配从包含a的行到包含b的行 sed -n ' ...

  10. 空间Rm的任意两个范数都互相等价