题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合。问总共有多少个集合,以及每个集合有多少人,并按从大到小输出。

  很明显,采用并查集。vis[k]标记爱好k第一次出现的人的编号,如果为0则表示未出现。

  当前第i个人若也存在爱好k,则只要将i与vis[k]两个人合并即可。

最后father[i]相同的即处在同一个集合中。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
/*
并查集
*/
const int maxn=;
int vis[maxn]; //vis[i]标记某爱好第一次出现在第几个人,0表示还未出现。
int n; struct UF{
int father[maxn];
void init(){
for(int i=;i<maxn;i++){
father[i]=i;
}
}
int find_root(int x){
if(father[x]!=x){
father[x]=find_root(father[x]);
}
return father[x];
}
void Union(int x,int y){
int fx=find_root(x);
int fy=find_root(y);
if(fx!=fy){
father[fy]=fx;
}
}
}uf; bool cmp(int a,int b){
return a>b;
}
int main()
{
char str[];
int num,a;
scanf("%d",&n);
memset(vis,-,sizeof(vis));
uf.init();
for(int i=;i<=n;i++){
scanf("%s",str);
int len=strlen(str);
str[len-]='\0';
num=atoi(str);
for(int k=;k<num;k++){
scanf("%d",&a);
if(vis[a]==-)
vis[a]=i;
else{
uf.Union(vis[a],i); //若已出现过,则第i个人与第vis[a]个人合并,即分为一组
}
}
}
int cnt[n+];
int res=;
memset(cnt,,sizeof(cnt));
for(int i=;i<=n;i++){
int idx=uf.find_root(i);
if(cnt[idx]==)
res++;
cnt[idx]++;
}
sort(cnt+,cnt+n+,cmp);
printf("%d\n",res);
for(int i=;i<res;i++)
printf("%d ",cnt[i]);
printf("%d",cnt[res]);
return ;
}

PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)的更多相关文章

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

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

  5. PAT (Advanced Level) 1107. Social Clusters (30)

    简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  6. PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)

    题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径.   对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的.   如果最短路程 ...

  7. 1107. Social Clusters (30)

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

  8. PAT甲级——1107 Social Clusters (并查集)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30  ...

  9. PAT-1107 Social Clusters (30 分) 并查集模板

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

随机推荐

  1. [2018HN省队集训D9T1] circle

    [2018HN省队集训D9T1] circle 题意 给定一个 \(n\) 个点的竞赛图并在其中钦定了 \(k\) 个点, 数据保证删去钦定的 \(k\) 个点后这个图没有环. 问在不删去钦定的这 \ ...

  2. 本博客已不再更新,新文章将发布在我的个人博客:https://www.tapme.top

    如题,本博客已不再更新,请访问个人博客:www.tapme.top

  3. BZOJ4154:[Ipsc2015]Generating Synergy(K-D Tree)

    Description 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问点a的颜色 Input 第一行一个数T,表示数据组数 接下来每组数据的第一行三 ...

  4. oracle常见受权与回收权限 grant和revoke

    1.GRANT 赋于权限 常用的系统权限集合有以下三个: CONNECT(基本的连接),   RESOURCE(程序开发),   DBA(数据库管理) 常用的数据对象权限有以下五个: ALL   ON ...

  5. Python2.7-anydbm

    anydbm模块,把各种数据库模块(dbhash (requires bsddb), gdbm, or dbm)的接口进行了统一.打开后返回的对象操作和字典类似 模块方法: anydbm.open(f ...

  6. JavaScript HTML DOM,BOM

    DOM DOM 是一个 W3C (万维网联盟) 标准. DOM 定义了用于访问文档的标准: "W3C 文档对象模型 (DOM) 是一个平台和与语言无关的界面, 允许程序和脚本动态访问和更新文 ...

  7. 【转】VISUAL STUDIO 2008代码指标为您节省资金

    转自:https://www.geekzone.co.nz/vs2008/4773 Visual Studio 2008 Team Developer和Team Suite版本中提供的许多新功能之一是 ...

  8. jqgrid 分页时,清空原表格数据加载返回的新数据

    由于,我们是动态分页,分页后的数据是在触发分页后动态加载而来.如何使jqgrid清空原数据而加载新数据? 1)调用jqgrid的 clearGridData 方法清空表格数据 2)调用jqgrid的  ...

  9. ASP.NET Core的Kestrel服务器(转载)

    Kestrel是一个基于libuv的跨平台ASP.NET Core web服务器,libuv是一个跨平台的异步I/O库.ASP.NET Core模板项目使用Kestrel作为默认的web服务器.Kes ...

  10. Android Notification的使用 - z

    http://blog.csdn.net/new_one_object/article/details/55511253 另,博主其它文章也很好