题意:给出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. VFIO简介

    VFIO是一套用户态驱动框架,它提供两种基本服务: 向用户态提供访问硬件设备的接口 向用户态提供配置IOMMU的接口 VFIO由平台无关的接口层与平台相关的实现层组成.接口层将服务抽象为IOCTL命令 ...

  2. SQLite 插入大量数据慢的解决方法

    sqlite 插入数据很慢的原因:sqlite在没有显式使用事务的时候会为每条insert都使用事务操作,而sqlite数据库是以文件的形式存在磁盘中,就相当于每次访问时都要打开一次文件,如果对数据进 ...

  3. GO学习笔记:函数传值与传指针

    当我们传一个参数值到被调用函数里面时,实际上是传了这个值的一份copy,当在被调用函数中修改参数值的时候,调用函数中相应实参不会发生任何变化,因为数值变化只作用在copy上. 为了验证我们上面的说法, ...

  4. GO学习笔记:函数Panic和Recover

    Go没有像Java那样的异常机制,它不能抛出异常,而是使用了panic和recover机制.一定要记住,你应当把它作为最后的手段来使用,也就是说,你的代码中应当没有,或者很少有panic的东西.这是个 ...

  5. Django进阶Admin篇 - admin基本配置

    django admin 是django自带的一个后台app,提供了后台的管理功能. 基础知识点: 一.认识ModelAdmin 管理界面的定制类,如需扩展特定的model界面,需要从该类继承 二.注 ...

  6. 如何让pycharm以py.test方式运行

    第一步:进入File—Settings—Python Integrated Tools 发现设置中Default test runner是Unittests 将其改为py.test,点击OK保存 如果 ...

  7. poj2723 2-sat

    当两个门锁相同时,这个钥匙必须用,不同时分开用 可以直接遍历门,当然二分更快 #include<map> #include<set> #include<cmath> ...

  8. 算法练习5---快速排序Java版

    基本思想:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成 ...

  9. poj 1787 背包+记录路径

    http://poj.org/problem?id=1787 Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Subm ...

  10. 广义线性模型(GLM)

    一.广义线性模型概念 在讨论广义线性模型之前,先回顾一下基本线性模型,也就是线性回归. 在线性回归模型中的假设中,有两点需要提出: (1)假设因变量服从高斯分布:$Y={{\theta }^{T}}x ...