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 (≤), 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​​ (>) is the number of hobbies, and [ 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

思路:

有n个人,每个人喜欢k个活动,如果两个人有任意一个活动相同,就称为他们处于同一个社交网络。求这n个人一共形成了多少个社交网络。

 
#include <bits/stdc++.h>
using namespace std;
int p[],a[];
int n,k,m,h;
char c;
bool cmp(int a,int b)
{
return a>b;
} int found(int x)
{
if(p[x] == x)
return x;
return found(p[x]);
}//found函数返回的是
void unite(int a,int b)
{
int x = found(a);
int y = found(b);
if(x!=y)
{
p[y] = x;
}
}//unite函数
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
p[i] = i;
for(int i=;i<=n;i++)
{
scanf("%d %c",&k,&c);
for(int j=;j<=k;j++)
{
scanf("%d",&h);
if(a[h] == )
{
a[h] = i;
}
else
{
unite(found(a[h]),i);
}
}
}
int cnt = ;
int count1[] = {};
for(int i=;i<=n;i++)
{
count1[found(i)]++;
}
for(int i=;i<=n;i++)
{
if(count1[i]!=)
cnt++;
}
cout<<cnt<<endl;
sort(count1+,count1+n+,cmp);
for(int i=;i<=cnt;i++)
{
if(i==)
cout<<count1[i];
else
cout<<" "<<count1[i];
}
return ;
}
 

1107 Social Clusters (30 分)的更多相关文章

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

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

  2. PAT-1107 Social Clusters (30 分) 并查集模板

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

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

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

  4. 1107 Social Clusters (30)(30 分)

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

  5. 1107. Social Clusters (30)

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

  6. pat甲级 1107. Social Clusters (30)

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

  7. PAT (Advanced Level) 1107. Social Clusters (30)

    简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  8. PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)

    题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...

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

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

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

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

随机推荐

  1. RestTemplate请求

    JSONObject json = new JSONObject(sendParam);HttpHeaders headers = new HttpHeaders();MediaType type = ...

  2. 脱了裤子放屁之std::string

    一个天天跟c#奋斗的苦逼c++程序猿 改自己曾经代码的时候发现有例如以下几行. char szPath[MAX_PATH] = {0}; GetModuleFileNameA(NULL,szPath, ...

  3. TCP协议和socket API 学习笔记

    本文转载至 http://blog.chinaunix.net/uid-16979052-id-3350958.html 分类:  原文地址:TCP协议和socket API 学习笔记 作者:gilb ...

  4. SQuirreL – Phoenix的GUI

    本文主要介绍如何通过SQuirreL访问Phoenix,以及如何在SQuirreL中配置Phoenix参数. 什么是SQuirrel? SQuirreL SQL Client是一个开源免费软件, 可以 ...

  5. xmanager 5图文使用教程

    1.Xconfig xconfig是linux下X Window环境中用于配制的一个工具,和menuconfig相似,但用法更友好方便. 当你创建一个会话,会话分配一个默认的配置文件.Xmanager ...

  6. iOS应用的入口自定义和事件处理的自定义

    iOS应用的入口自定义和事件处理的自定义 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: ...

  7. Redis C语言操作封装

    #ifndef BOYAA_FOURLANDLORD_REDISCLASS_H_20130217 #define BOYAA_FOURLANDLORD_REDISCLASS_H_20130217 #i ...

  8. dhcpcd守护进程分析【转】

    本文转载自;http://blog.csdn.net/lishanmin11/article/details/37930073 最近在调android ethernet功能,android本身不带 e ...

  9. PYTHON 爬虫笔记三:Requests库的基本使用

    知识点一:Requests的详解及其基本使用方法 什么是requests库 Requests库是用Python编写的,基于urllib,采用Apache2 Licensed开源协议的HTTP库,相比u ...

  10. codeforces A. Fox and Box Accumulation 解题报告

    题目链接:http://codeforces.com/problemset/problem/388/A 题目意思:有 n 个 boxes,每个box 有相同的 size 和 weight,但是stre ...