简单并查集。

#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. Android Lights

    Android Lights 很多Android手机上都配有LED灯,手机在充电.新来短信等时候都会有相应的指示灯提示. Android系统之中,一共定义了8个逻辑灯,包含:背光,键盘灯,按键灯,充电 ...

  2. NFS挂载故障卡死的问题

    NFS挂载故障卡死的问题 默认是硬的,改成软的.比如:mount -t nfs -o rw,vers=4,noacl,nocto,noatime,nodiratime,rsize=131072,wsi ...

  3. Python 学习笔记12

    不积跬步,无以至千里.不积小流,无以成江河. 当我觉得沮丧.绝望的时候,就疯狂的敲代码,这样会好受一点. 今天和昨天敲了两天的小程序,算是对python的具体语法规则有个初步的手熟. http://w ...

  4. 将数组写入plist文件

    data 加载plist [NSBundle mainBundle] [arr writeToURL:<#(NSURL *)#> atomically:<#(BOOL)#>]

  5. webstorm2016.2.4激活码

    测试日期:2016.11.07 webstorm版本:2016.2.4 系统环境:MacOS(windows环境应该也行) 激活码如下: 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0 ...

  6. Thrift源码解析--TBinaryProtocol

    本文为原创,未经许可禁止转载. 关于Tprotocol层都是一些通信协议,个人感觉内容较大,很难分类描述清楚.故打算以TBinaryProtocol为例,分析客户端发请求以及接收服务端返回数据的整个过 ...

  7. hibernate的批量更新、批量删除

    hibernate的批处理API:session.createQuery(hql).executeUpdate(),如果有参数则在执行之前设置参数. 批量更新示例: @Test public void ...

  8. A. Brain's Photos ——Codeforces Round #368 (Div. 2)

    A. Brain's Photos time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. 初次使用IntelliJ IDEA 2016.2

    换电脑的还有一个目的就是我准备采用新的IDE了 之前一直用的是myeclipse,但是现在准备尝试idea 这边做个记录,idea的破解参考下面这个网址:http://blog.csdn.net/u0 ...

  10. ContentPlaceHolderID属性

    用来对应包含与当前内容关联的 ContentPlaceHolder 的 ID啊 说白了就是去找母版页相应的ContentPlaceHolder ,然后把内容扔进那里面去 <asp:Content ...