题意:有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. 每年有20万人进军IT行业,为何还会人才短缺?

    众所周知,IT行业是个高薪行业,也是很多人的梦想职业,在全球最缺人的十大行业中IT行业居首位. 但是现在很多人都有一个疑问: 几乎每所大学里都有计算机技术相关专业,再加上IT培训机构的输出,每年培养出 ...

  2. 使用SuperMap对接天地图

    我们在做项目时,经常需要与天地图对接,对接形式分为2种: 1. 将公网天地图用作项目底图,在JavaScript客户端加载显示: 2. 将自己发布的WMTS地图服务给别人用,同时需要自己的服务能和天地 ...

  3. Netty入门(二)时间服务器及客户端

    在这个例子中,我在服务器和客户端连接被创立时发送一个消息,然后在客户端解析收到的消息并输出.并且,在这个项目中我使用 POJO 代替 ByteBuf 来作为传输对象. 一.服务器实现 1.  首先我们 ...

  4. springbatch入门练习(第一篇)

    先搞懂几个概念 Job Respository: 作业仓库,负责Job.Step执行过程中的状态保存 Job launcher: 作业调度器,提供执行Job的入口 Job:作业,由多个step组成,封 ...

  5. 关于css浮动的一点思考

    浮动到底是什么? 浮动核心就一句话:浮动元素会脱离文档流并向左/向右浮动,直到碰到父元素或者另一个浮动元素.请默念3次! 浮动最初设计的目的并没那么多事儿,就只是用来实现文字环绕效果而已,如下所示: ...

  6. C++之强制类型转化

    在C++语言中新增了四个关键字static_cast.const_cast.reinterpret_cast和dynamic_cast.这四个关键字都是用于强制类型转换的.我们逐一来介绍这四个关键字. ...

  7. C#构造方法--实例化类时初始化的方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  8. NSDictionary实现原理-ios哈希hash和isEqual

    NSDictionary实现原理-ios哈希hash和isEqual   OC中自定义类的NSCopying实现的注意事项(isEqual & hash实现) http://blog.csdn ...

  9. scala-数组操作

    package com.bigdata import scala.collection.mutable.ArrayBuffer object ArrayO { def main(args: Array ...

  10. Vivado中xilinx_courdic IP核(求exp指数函数)使用

    由于Verilog/Vhdl没有计算exp指数函数的库函数,所以在开发过程中可利用cordic IP核做exp函数即e^x值: 但前提要保证输入范围在(-pi/4—pi/4) 在cordic核中e^x ...