题意:给出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. HTML5笔记学习(canvas)

    来源于<HTML5高级程序设计> css3圆角 border-radius旋转变换 transform:rotate(); 变换 transformation动画 animation过度 ...

  2. shell awk命令

    语法: awk '{command}' filename  多个命令以分号分隔. awk 'BEGIN {command1} {command2} END{command3}'  注意:BEGIN , ...

  3. 编译android源码中的icu4c

    在external/icu4c/studata/readme.txt,里面有修改icu4c中资源的编译方法 # 具体步骤(可复制下面命令,直接运行): # 1)新增或者修改external/icu4c ...

  4. How to Fix “ShellExecute failed (2): Is this command correct?” on Notepad++

    Problem: When you click right-click->Edit with Notepad ++ and get the error “ShellExecute failed ...

  5. 异步请求(ajax,http) 之 逐渐完善的大全

    异步请求在我们的开发之中是经常需要学习和理解的内容,我们将会在这一篇文章中依据不同的语言和环境内容进行归类讲解. JS: ajax是我们最为常用的页面异步请求,在只需要修改部分页面内容而不需要更换全部 ...

  6. js区分字符串和数字,有时候需要将字符串转换成数字

    js区分字符串和数字,有时候需要将字符串转换成数字 :parseInt

  7. LeetCode OJ:Remove Duplicates from Sorted List (排好序的链表去重)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  8. 2017.11.7 Python 制作EFM32/ AVR批量烧录工具

    Customer need program quickly asap. ok,I need to set up a table for test. 1 reference data http://ww ...

  9. c# unicode 编码 中文转换 已测试(转)

    中文传参时因为编码不同经常凌乱风中,故传前编成unicode码来过度是一个不错的解决方法 /// <summary> /// 中文转unicode        /// </summ ...

  10. PhotoShop脚本指南

    Photoshop脚本语言 Photoshop支持三种脚本语言:AppleScript,VBScript,JavaScript.其中AppleScript为苹果系统,VBScript为Windows操 ...