• PAT 甲级 A1107 Social Clusters


    When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. You are supposed to find all the clusters.

    Input Specification:

    Each input file contains one test case. For each test case, the first line contains a positive integer N (≤1000), the total number of people in a social network. Hence the people are numbered from 1 to N. Then N lines follow, each gives the hobby list of a person in the format:

    Ki​: hi​[1] hi​[2] ... hi​[Ki​]

    where Ki​ (>0) is the number of hobbies, and hi​[j] is the index of the j-th hobby, which is an integer in [1, 1000].

    Output Specification:

    For each case, print in one line the total number of clusters in the network. Then in the second line, print the numbers of people in the clusters in non-increasing order. The numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

    Sample Input:

    1. 8
    2. 3: 2 7 10
    3. 1: 4
    4. 2: 5 3
    5. 1: 4
    6. 1: 3
    7. 1: 4
    8. 4: 6 8 1 5
    9. 1: 4

    Sample Output:

    1. 3
    2. 4 3 1

    题意:

    有n给人物,遍历这n个人物,每个人物有k项喜欢得运动,在:后面引出,如果这些人喜欢的活动有交集,则这些人物在一个社交圈内,问有几个社交圈?每个社交圈内有多少人,按从大到小的排列顺序输出;

    思路:
    找元素有关系的集合,用并查集,但是这个题目给的不是直接的人物和人物的关系,而是人和喜欢活动的关系,因此要将第一个喜欢该活动的人记录下来作为人和喜欢活动的树的根节点,某个人可以通过这个活动树可以找到人和人之间的关系。

    代码:
     

    1. //该题没有直接给出人物关系,而是通过活动间接找到人物关系;
    2. //不是通过人与人之间的直接关系,而是通过喜欢的活动而实现的间接关系;
    3. #include
    4. #include
    5. using namespace std;
    6. const int maxn = 1010;
    7. int course[maxn] = {0};
    8. int father[maxn] = {0};
    9. int isroot[maxn] = {0};
    10. int n, m, k, h;
    11. bool cmp(int a, int b) {
    12. return a > b;
    13. }
    14. int findFather(int x) {//找到父节点才能判断是否在一个集合内;
    15. if(father[x] == x) return x;
    16. else {//递归找根节点,路径压缩;
    17. int f = findFather(father[x]);
    18. father[x] = f;
    19. return f;
    20. }
    21. }
    22. void Union(int a, int b) {
    23. int fa = findFather(a);
    24. int fb = findFather(b);
    25. if(fa != fb){
    26. father[fb] = fa;
    27. }
    28. }
    29. void init(int n) {
    30. for(int i = 1; i <= n; i++){
    31. father[i] = i;//初始化,使得每个结点都暂时拥有自己得一个集合;
    32. }
    33. }
    34. int main ( ){
    35. scanf("%d", &n);//有n个人
    36. init(n);//初始化,保证每个集合至少有一个人;
    37. for(int i = 1; i <= n; i++) {
    38. scanf("%d:", &k);//这个人有k项喜欢的活动;
    39. for(int j = 0; j < k; j++) {
    40. scanf("%d", &h);//喜欢的活动h;
    41. if(course[h] == 0) {
    42. course[h] = i;//通过活动间接来建立关系,因此要建立一颗人物与活动的关系树;
    43. }
    44. Union(i, findFather(course[h]));//通过人物间接找到人物关系;
    45. }
    46. }
    47. for(int i = 1; i <= n;i++) {//遍历n个人物,统计每个集合内人的个数;
    48. isroot[findFather(i)]++;
    49. }
    50. int ans=0;
    51. for(int i = 1; i <= n; i++) {
    52. if(isroot[i] != 0) ans++;
    53. }
    54. printf("%d\n", ans);
    55. sort(isroot + 1, isroot + 1+n, cmp);
    56. for(int i = 1; i<=ans; i++) {
    57. printf("%d", isroot[i]);
    58. if(i != ans) printf(" ");
    59. }
    60. return 0;
    61. }

  • 相关阅读:
    js 内存泄露,几种常涉及到的内存泄露
    document.body.clientHeight获取可视区域高度为0问题解决
    使用go-redis/redis依赖操作redis
    2023-2028年中国硫氰酸钠市场发展动态及前景预测报告
    Pytorch实战教程(三)-构建神经网络
    反射学习总结
    深入理解计算机系统(CSAPP) —— 第二章 信息的表示和处理
    【Linux】《Linux命令行与shell脚本编程大全 (第4版) 》笔记-Chapter25-井井有条
    【牛客刷题】
    Vue路由组件的缓存keep-alive和include属性
  • 原文地址:https://blog.csdn.net/m0_51711089/article/details/125897117