简单并查集。

#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. wpf 数据绑定的4种形式

    1.source 2.element 3.relativesource 4.datacontent

  2. stm32菜单按键的设计

    有点懒.看注释吧 // k0,enter/enable;k3:esc/disable// k1,value+/menu+;k2:menu-/value-; #include "sysmenu ...

  3. HDU - 1045 Fire Net(二分匹配)

    Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...

  4. HDU 5773 The All-purpose Zero

    这题想了1个多小时想不出来...方法真是精妙... 官方题解:0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的.因此我们可以把0拿出来,对剩下 的做O(nlogn)的LIS, ...

  5. linux上安装配置samba服务器

    linux上安装配置samba服务器 在linux上安装配置samba服务器 在这给大家介绍一个不错的家伙,samba服务.如果您正在犯愁,如何在Windows和Linux之间实现资源共享,就请看看这 ...

  6. HDU2206:IP的计算

    Problem Description 在网络课程上,我学到了很多有关IP的知识.IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方 ...

  7. UI篇—UITableview

    一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支 ...

  8. web.xml文件中的7个错误的安全配置

    web.xml文件中的7个错误的安全配置 关于Java的web.xml文件中配置认证和授权有大 量 的 文章.本文不再去重新讲解如何配置角色.保护web资源和设置不同类型的认证,让我们来看看web.x ...

  9. 剑指offer青蛙跳台阶问题

    (1)一只青蛙一次可以跳上 1 级台阶,也可以跳上2 级.求该青蛙跳上一个n 级的台阶总共有多少种跳法. //递归方式  public static int f(int n) { //参数合法性验证 ...

  10. windows的DOS窗口如何修改大小

    关于这个问题,其实很简单.不知道为什么网上的资料乱遭的.故自己写下来,方便有不明白的童鞋参考. 左键点击左上角的区域会弹出一个菜单,选择属性. 如下图就能轻松的修改窗口的大小了.