简单并查集。

#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. POJ 2062 HDU 1528 ZOJ 2223 Card Game Cheater

    水题,感觉和田忌赛马差不多 #include<cstdio> #include<cstring> #include<cmath> #include<algor ...

  2. 总结的git操作命令小抄集

    .gitignore 本地仓库主目录下,用于定义提交时忽略的文件   git add <file-name> 将修改或新增的文件存入暂存区   git reset HEAD <fil ...

  3. ios随机数

    ios 随机数生成 字数612 阅读3037 评论1 喜欢15 最近一直使用随机数,为了以后方便查阅,总结一下: 在C中提供了rand().srand().random().arc4random()几 ...

  4. php错误记录

    1.模板不存在ThinkPHP\Library\Think\View.class.php LINE: 110 是因为IndexController的Index函数,而View中没有对应的Index文件 ...

  5. gerrit review 设置

    $ git config remote.review.pushurl "ssh://someone@ip:29418/the_project" $ git config remot ...

  6. 通过C++修改系统时间代码

    #include <windows.h>#include <stdio.h>#include <iostream>using namespace std;int m ...

  7. hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  8. php中的PHP_EOL换行符

      看手册时发现PHP_EOL这个变量,查了下资料,原来是相当于换行符 在unix系列用 \n 在windows系列用 \r\n 在mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代 ...

  9. JSP基础语法--跳转指令 jsp:forward page

    带参数的跳转指令: <jsp:forward page="{路径|<%=表达式%>}"/> <jsp:param name="参数名称&qu ...

  10. sql定期移植数据的存储过程

    create PROCEDURE [dbo].[Pro_ZT_SYS_LogInfo_clear] @dt_end datetime --清理此日期之前的数据 AS BEGIN SET NOCOUNT ...