题意:给出n个人(编号为1~n)以及每个人的若干个爱好,把有一个或多个共同爱好的人归为一个集合,问共有多少个集合,每个集合里有多少个人?

思路:典型的并查集题目。并查集的模板init()函数,union()函数,findSet()函数就不多讲了。这里根据爱好来归类,因此,在读入数据时把爱好进行合并。设置数组hobby[],hobby[id]表示编号为id的这个人的一个爱好,如果某个人有多个爱好,只要记录一个就好了;设置数组num[],num[fa]表示以fa为根结点的爱好集合的人数,初始化为0。然后遍历n个人(人的编号的1~n),对于每个人i,因为我们已经记录了这个人的一个爱好(即hobby[i]),故可以找到该爱好所属的集合的根结点(即int fa=FindSet(hobby[i])),然后令num[fa]++即可。统计好每个集合的人数之后,再统计共有多少个集合,只需要遍历所有爱好(爱好的编号为1~1000),记录num[i]不为0的个数即可。

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
;
int hobby[maxn];//hobby[id]记录id的任意一个爱好
};//num[fa]记录以fa为根的集合的结点个数
int father[maxn];
bool cmp(int a,int b){return a>b;}

void Init(){
    ;i<maxn;i++)
        father[i]=i;
}

int FindSet(int a){
    int root=a;
    while(root!=father[root])
        root=father[root];
    while(a!=father[a]){
        int tmp=a;
        a=father[a];
        father[tmp]=root;
    }
    return root;
}

void Union(int a,int b){
    int setA=FindSet(a);
    int setB=FindSet(b);
    if(setA!=setB) father[setB]=setA;
}

int main()
{
    Init();
    int n,k;
    scanf("%d",&n);
    ;id<=n;id++){
        int pre,curr;
        scanf("%d:%d",&k,&pre);
        hobby[id]=pre;//记录第i个人的一个爱好
        ;j<k;j++){
            scanf("%d",&curr);
            Union(pre,curr);
            pre=curr;
        }
    }
    //遍历n个人,统计每个集合的人数
    ;id<=n;id++)
        num[FindSet(hobby[id])]++;
    //遍历所有爱好,统计集合的个数
    ;
    ;i<maxn;i++)
        ) cnt++;
    sort(num,num+maxn,cmp);
    printf("%d\n",cnt);
    ;i<cnt;i++){
        printf("%d",num[i]);
        ) printf(" ");
    }
    ;
}

1107 Social Clusters的更多相关文章

  1. [并查集] 1107. Social Clusters (30)

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

  2. 1107 Social Clusters[并查集][难]

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

  3. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

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

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

  5. 1107 Social Clusters——PAT甲级真题

    1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...

  6. 1107. Social Clusters (30)

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

  7. 1107 Social Clusters (30)(30 分)

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

  8. 1107 Social Clusters (30 分)

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

  9. pat甲级 1107. Social Clusters (30)

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

  10. PAT 1107 Social Clusters

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

随机推荐

  1. node基础(一)——http模块

    一.http模块 http.createSverver() http是node自带的模块,通过require("http")的方法载入: 使用http创建服务器: http.cre ...

  2. vmware增加共享文件夹

    增加共享文件夹 VMWare提供共享文件夹功能.前提是在虚拟机中安装VMware tools 1. 安装VMware tools 会自动在虚拟机中的/media/VMware Tools/中有个压缩包 ...

  3. jquery.js和jquery.min.js的区别介绍

    1.区别:jquery官网提供2种jQuery的下载,一种是jquery.js另一种是jquery.min.js文件名不一定完全相同,但通常情况下:jquery.js是完整的未压缩的jQuery库,文 ...

  4. Docker 应用实例

    Docker安装Nginx 方法一.通过 Dockerfile构建 创建Dockerfile 首先,创建目录nginx,用于存放后面的相关东西. runoob@runoob:~$ mkdir -p ~ ...

  5. go 编译

    linux: set GOARCH=amd64 set GOOS=linux go build -o app_name main.go echo "编译完成,任意键退出" paus ...

  6. 影响wifi信号强度因素

    影响WIFI信号强度因素: 1.室内错综复杂的环境,会在一定程度上导致WIFI无线信号产生多径传播现象,从而导致信号强度的不稳定性: 2.室内的人员密集程度和人员流动情况也会对WIFI信号强度产生影响 ...

  7. MVC框架中的值提供机制(二)

    在MVC框架中存在一些默认的值提供程序模板,这些值提供程序都是通过工厂模式类创建;在MVC框架中存在需要已Factory结尾的工厂类,在值提供程序中也存在ValueProviderFactories工 ...

  8. web前端概念摘要(一)

    1.前端不必等后端开发完成后才开发的情况:(1)前后端分离:前后端工程不在同一工程目录,前端专注页面样式与效果开发,设计数据展示等问题,可自行建立假数据或本地数据文件测试.后期联调再做修改,修改前端人 ...

  9. JavaScript 哈希表(散列表)实现和应用

    查找的效率与比较次数密切相关.基于比较的程序,运算效率是比较低的.比如平时可以通过indexOf查找一个数据.但这是一个基于比较的一个实现.如果是淘宝那样有上亿个商品,那么用indeOf 来查数据就会 ...

  10. 深度学习(六十五)移动端网络MobileNets