题意:有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. js将时间戳转换成日期格式-陈远波

    var timestamp =1539598555000;//时间戳 //时间戳转换成time格式function timestampToTime(timestamp) { var date = ne ...

  2. Spirng MVC 重定向传递对象

    在 Spring MVC 中我们会经常遇到重定向. @RequestMapping("/order/saveorder.html") public String saveOrder ...

  3. DAU、UV、独立IP、PV的区别和联系

    基本概念 DAU(Daily Active User)日活跃用户数量.常用于反映网站.互联网应用或网络游戏的运营情况.DAU通常统计一日(统计日)之内,登录或使用了某个产品的用户数(去除重复登录的用户 ...

  4. Android权限申请完全解析(一):Android自带的权限申请

    1.为什么要权限申请 6.0以上就需要了,别问为什么.(不是重点,自行搜索) 2.如何进行权限申请 Android自带的权限申请 EasyPermission权限申请 Ps:EasyPermissio ...

  5. Oracle_spatial的常见错误与注意事项

    常见的错误 1.ORA-13226:没有空间索引接口将不被支持 当使用一个空间操作符时,如果没有使用空间索引导致该操作符不能被完成将会返回该错误.这可能会发生在当你使用的列上没有空间索引.或者优化器没 ...

  6. jQuery UI dialog插件出错信息:$(this).dialog is not a function

    使用jQuery UI 1.7.2 dialog插件,遇到这样的错误: [img]http://dl.iteye.com/upload/attachment/308166/97c428e4-2ce2- ...

  7. python string.md

    string 包含用于处理文本的常量和类.string模块始于Python的最早版本. 2.0版本中, 许多之前只在模块中实现的函数被转移为string对象的方法. 之后的版本中, 虽然这些函数仍然可 ...

  8. jQuery 实现复选框的全选与反选

    <script> //实现全选与反选 $(".allAndNotAll").click(function () { if ($(this).prop("che ...

  9. 关于Netty Pipeline中Handler的执行顺序问题

    原文地址:http://blog.csdn.net/wgyvip/article/details/25637651 最近在学习Netty框架,根据官网的教程学着做了几个小测试,都成功了,后面开始试着写 ...

  10. iframe-metamask

    iframe--require('iframe') higher level api for creating and removing iframes in browsers 用于创建或移除浏览器中 ...