题意:给出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. 资源(GitHub)

    angular 文档 https://angular.cn/docs/ts/latest/quickstart.html   http://sc.qq.com/fx/u?r=OfFE5AA ios h ...

  2. python学习笔记(conf配置文件)

    在优化自己的框架中发现一个问题 有很多参数在很多类中都要使用.是否有什么功能可以帮助优化这些功能 这里我就想到 conf配置文件.整理了下资料 总结下内容如下 #!/usr/bin/env pytho ...

  3. SSH登陆阿里云服务器出现Permission denied (publickey)错误解决方案

    操作环境: 操作系统:Mac10.11.5 阿里云服务器:Ubuntu16.04 远程连接:SSH 注:首先我们已假设你已经自己生成了SSH秘钥,并已经配置到阿里云.绑定了自己的云服务器. 但是后来发 ...

  4. .net操作Oracle数据库步骤及方法

    1.首先安装PL/SQL Developer Oracle客户端软件 2.安装Oracle Instant Client(即时客户端) 安装与配置 配置环境变量ORAClE HOME 地址为insta ...

  5. Intellij IDEA 配置Subversion插件实现步骤详解

    在使用Intellij的过程中,突然发现svn不起效了,在VCS–>Checkout from Version Control中也未发现Subversion这一项.如下图:  一.原因查找 经过 ...

  6. lucas定理学习

    Lucas定理是用来求 c(n,m) mod p,p为素数的值. 表达式: C(n,m)%p=C(n/p,m/p)*C(n%p,m%p)%p 当我们遇到求一个N,M很大的组合数的时候,递推法就显得很耗 ...

  7. STM32F407: USART 遇到的问题

    今天初次使用STM32F407进行USART串口通讯实验,按照f103的代码写完了,发现没法发送数据, 查看文档后发现是由于没有将端口映射到USART1,然后添加如下代码: 1 GPIO_PinAFC ...

  8. mac/linux ssh 免密码登陆配置及错误处理

    先说一下,mac 和linux 的设置方法是一样的 一般做法可以参照http://www.tuicool.com/articles/i6nyei 第一步:生成密钥.在终端下执行命令: ssh-kege ...

  9. HihoCoder 1185 : 连通性·三(强连通缩点)

    连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出去,就拜托小Hi ...

  10. 安装Oracle数据库操作步骤

    第一步: 第二步: 第三步: 第四步: 第五步:输入密码 第六步:继续 第七步: 第八步:进入主页后 第九步:登录进去后是这样子 第十步: 第十一步: 第十二步: 最后一步:看到桌面上有个图标就说明安 ...