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:

Ki: hi[1] hi[2] ... hi[Ki]

where Ki (>0) is the number of hobbies, and hi[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 <cstdio>
#include <vector>
#include <algorithm>
using namespace std; vector<int> father(), num();
int cnt;//记录集合数 //sort()从大到小排序比较函数
bool cmp(int a, int b){
return a > b;
} //初始化,每个人属于不同集合
void init(int n){
for(int i = ; i <= n; i++){
father[i] = i;
}
} int GetParent(int x){
if(x == father[x])
return x;
father[x] = GetParent(father[x]);//路径压缩
return father[x];
} void merge(int x, int y){
int fx = GetParent(x);
int fy = GetParent(y);
if(fx != fy){
cnt--;//不属于同一集合,合并之后集合数减一
father[fy] = fx;
}
} int main(){
int n, k, h, i;
int hobby[] = {};
scanf("%d", &n); //调用初始化函数
init(n); cnt = n;//初始化集合数为n //处理输入数据,合并集合
for(i = ; i <= n; i++) {
scanf("%d:", &k);
for(int j = ; j < k; j++) {
scanf("%d", &h);
if(hobby[h] == )
hobby[h] = i;
merge(hobby[h], i);
}
}
for(i = ; i <= n; i++)
//这里一定要注意,要找到i所属集合的根节点,num加一,num[father[i]]++是有问题的,因为
//这棵树深度不一定是2.。。即father[i]不一定是i所属集合的根节点
num[GetParent(i)]++; printf("%d\n", cnt); sort(num.begin(), num.end(), cmp); printf("%d", num[]);
for(i = ; i < cnt ; i++)
printf(" %d", num[i]);
return ;
}
 

最新文章

  1. iOS 二维码扫描
  2. python中使用sub替换字符串中的元素
  3. leetcode:Remove Linked List Elements
  4. 数据抓取的艺术(一):Selenium+Phantomjs数据抓取环境配置
  5. html 设置Select options值进行绑定
  6. Gradle一分钟实现Spring-MVC
  7. 自制单片机之十七……PC与单片机RS-232串口的通讯和控制
  8. Information seeking letter, hard copy version
  9. tasklet和工作队列
  10. 从头开始搭建一个Spring boot+RabbitMQ环境
  11. Cousera课程Learning How to Learn学习报告
  12. 201521123119《Java程序设计》第9周学习总结
  13. Mongodb3.0.5副本集搭建及spring和java连接副本集配置
  14. Django 2.0 新款URL配置详解
  15. Nginx实践篇(2)- Nginx作为静态资源web服务 - 控制浏览器缓存、防盗链
  16. 微信小程序采坑(一)
  17. JavaWeb学习 (五)————Servlet(一)
  18. P1802 5倍经验日(01背包问题,水题)
  19. 二叉查找树(二叉排序树)的详细实现,以及随机平衡二叉查找树Treap的分析与应用
  20. Navi.Soft31.开发工具(含下载地址)

热门文章

  1. bzoj 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复【最大生成树】
  2. Extjs6 经典版 combo下拉框数据的使用及动态传参
  3. fread/fwrite实现复制功能
  4. [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
  5. Uva 796 Critical Links (割边+排序)
  6. 题解报告:hdu 5695 Gym Class(拓扑排序)
  7. Kali linux 2016.2(Rolling)安装之后的常用配置
  8. 编写UI自动化测试用例原则
  9. EasyUI系列学习(十)-Tabs(选项卡)
  10. 修改 进程占用资源限制ulimit(限制服务器的链接数目)