PAT (Advanced Level) 1107. Social Clusters (30)
简单并查集。
#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)的更多相关文章
- 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- pat甲级 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
- 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 1107 Social Clusters (30)(30 分)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT (Advanced Level) 1111. Online Map (30)
预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...
- PAT (Advanced Level) 1103. Integer Factorization (30)
暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT (Advanced Level) 1072. Gas Station (30)
枚举一下选的位置,每次算一下就可以了. #include<cstdio> #include<cstring> #include<cmath> #include< ...
随机推荐
- Value '0000-00-00' can not be represented as java.sql.Date
Value '0000-00-00' can not be represented as java.sql.Date 时间 2014-07-30 09:00:50 ITeye-博客 原文 http: ...
- Asp.Net MVC 在后台获取PartialView、View文件生成的字符串
在Asp.net MVC的实际开发中,有时需要在后台代码中获取某个View 或者 PartialView 生成的字符串,示例如下: 1. 将View文件输出为字符串: /// <summary& ...
- <meta http-equiv="X-UA-Compatible" content="IE=Edge">
1.X-UA-Compatible X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中使用 ...
- Spring Timer实现
定时器:继承java.util.TimerTask类实现run方法 package com.zbb.framework.util.timer; import java.util.TimerTask; ...
- hdu 5493 Queue treap实现将元素快速插入到第i个位置
input T 1<=T<=1000 n 1<=n<=100000 h1 k1 h2 k2 ... ... hn kn 1<=hi<=1e9 0<=ki&l ...
- laravel性能优化
1. 配置信息缓存 使用以下 Artisan 自带命令,把 config 文件夹里所有配置信息合并到一个文件里,减少运行时文件的载入数量: php artisan config:cache 上面命令会 ...
- Android中的selector
android背景选择器selector用法汇总 (2011-04-19 13:40:00) 转载▼ 标签: android selector 背景选择器 it 分类: java/vb/Android ...
- 关于个人网站选择虚拟主机还是VPS服务器的讨论
还记得当初才开始学习建站的时候,选择的第一款虚拟主机是全HTML的主机,那时候的虚拟主机还分为HTML或者是ASP,PHP的都很少,在国内接触的学习较多还是以ASP为主,PHP是最近几年才开始流行.如 ...
- UVA 12083 POJ 2771 Guardian of Decency
/* http://acm.hust.edu.cn/vjudge/contest/view.action?cid=71805#problem/C */ 性质: [1]二分图最大点独立数=顶点数-二分图 ...
- unity3d继续尝试
这一次完成了一些复杂的脚本,会了一些简单的鼠标事件,这样就能使用鼠标进行简单的交互了. 其实右边栏目上面一些奇怪的属性看的我是眼花缭乱. 也不知道干啥用的,还有就是真的很佩服里面的物理引擎确实简单易上 ...