简单并查集。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; const int maxn=+;
int fa[maxn];
int num[maxn];
int n;
vector<int>g[maxn],ans; int f(int x)
{
if(x!=fa[x]) fa[x]=f(fa[x]);
return fa[x];
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int num; scanf("%d:",&num);
while(num--)
{
int hob; scanf("%d",&hob);
g[hob].push_back(i);
}
} for(int i=;i<=n;i++) fa[i]=i; for(int i=;i<=;i++)
{
if(g[i].size()==) continue;
int u=f(g[i][]);
for(int j=;j<g[i].size();j++)
{
int v=f(g[i][j]);
if(u!=v) fa[v]=u;
}
} for(int i=;i<=n;i++) f(i); memset(num,,sizeof num);
for(int i=;i<=n;i++) num[f(i)]++;
for(int i=;i<=n;i++) if(num[i]>) ans.push_back(num[i]); sort(ans.begin(),ans.end());
printf("%d\n",ans.size());
for(int i=ans.size()-;i>=;i--)
{
printf("%d",ans[i]);
if(i>) printf(" ");
else printf("\n");
} return ;
}

PAT (Advanced Level) 1107. Social Clusters (30)的更多相关文章

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

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

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

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

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

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

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

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

  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. 1107 Social Clusters (30)(30 分)

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

  7. PAT (Advanced Level) 1111. Online Map (30)

    预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...

  8. PAT (Advanced Level) 1103. Integer Factorization (30)

    暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  9. PAT (Advanced Level) 1072. Gas Station (30)

    枚举一下选的位置,每次算一下就可以了. #include<cstdio> #include<cstring> #include<cmath> #include< ...

随机推荐

  1. 网页 JavaScript

    今天,我主要学习了JavaScript的部分内容. JavaScript在html中的位置主要有三个地方:head里面.body里面.</html>之后,我们一般写在</html&g ...

  2. Oracle 获取当天数据

    where trunc(to_date(DATETIME,'yyyy-MM-dd hh24:mi:ss'))=trunc(sysdate)

  3. APP测试--功能测试

    1.1 了解需求 这一点,不但是功能测试,是所有测试都需要的第1步.通过需求文档,与产品经理的沟通,与开发的沟通,用户的使用习惯等各方法,了解APP的需求. 1.2 编写测试用例 当然之前可能是测试计 ...

  4. Python学习之旅--第一周--初识Python

    一:Python是一种什么样的语言? 1.语言的分类: a.编译型语言和解释性语言: 通常所说的计算机语言分为编译型和解释型语言.编译型语言典型的如C,C++,通常在程序执行之前必须经由编译器编译成机 ...

  5. UVALive 7070 The E-pang Palace(暴力)

    实话说这个题就是个暴力,但是有坑,第一次我以为相含是不行的,结果WA,我加上相含以后还WA,我居然把这两个矩形的面积加在一块了吗,应该取大的那一个啊-- 方法就是枚举对角线,为了让自己不蒙圈,我写了一 ...

  6. mac随手笔记

    在mac下安装有时候遇到一个问题,需要sudo指令来解决. sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,如halt,reboot,su等等.这 ...

  7. LeetCode OJ 26. Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  8. swap in java?

    Is it possible to write a swap method in java? these two variables will be primitives. It's not poss ...

  9. codeforces DIV2 D 最短路

    http://codeforces.com/contest/716/problem/D 题目大意:给你一些边,有权值,权值为0的表示目前该边不存在,但是可以把0修改成另外一个权值.现在,我们重新建路, ...

  10. shell中exec解析

    参考:<linux命令.编辑器与shell编程> <unix环境高级编程> exec和source都属于bash内部命令(builtins commands),在bash下输入 ...