题意:给出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. JDK__下载地址

    1. http://www.oracle.com/technetwork/java/archive-139210.html ZC: 貌似 从JDK7开始,有for ARM的版本,类似 : “Linux ...

  2. 教你打造一个Android组件化开发框架

    *本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 CC:Component Caller,一个android组件化开发框架, 已开源,github地址:https://github ...

  3. 异步编程——promise

    异步编程--promise 定义 Promise是异步编程的一个解决方案,相比传统的解决方法--回调函数,使用Promise更为合理和强大,避免了回调函数之间的层层嵌套,也使得代码结构更为清晰,便于维 ...

  4. 远程调用之RMI、Hessian、Burlap、Httpinvoker、WebService的比较

    一.综述 本文比较了RMI.Hessian.Burlap.Httpinvoker.WebService5这种通讯协议的在不同的数据结构和不同数据量时的传输性能. RMI是java语言本身提供的远程通讯 ...

  5. deep learning新征程(二)

    deep learning新征程(二) zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan  2016-4-5   声明 1)该Dee ...

  6. ubuntu下搭建hadoop平台

    终于把单击模式跟伪分布式模式搭建起来了,记录于此. 1.SSH无密码验证配置 因为伪分布模式下DataNode和NameNode均是本身,所以必须配置SSH localhost的无密码验证. 第一步, ...

  7. C++轮子队 敏捷冲刺

    团队Github地址:https://github.com/Pryriat/2048.git 敏捷开发——第1天 Alpha阶段第1次Scrum Meeting 敏捷开发起始时间 2018/10/27 ...

  8. Spring:org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class

    很长时间没有使用Spring,Hibernate,Struts2等一些框架了,现在使用起来还是有点陌生,今天刚弄就在Tomcat在启动的时候是报的这个错误: org.springframework.b ...

  9. react style: 二级菜单

    1.样式 @import "../../styles/varibles"; .app-sidebar { overflow: hidden; width: 180px; > ...

  10. switch遇到0的问题

    你是否经常有switch来代替if else?是否因为使用了switch,提高代码的执行效率而庆幸?好吧,你和我一样,但也许你没有遇到下面的问题. 这个小程序,会输出什么呢?会是'00'么?   结果 ...