1107. Social Clusters (30)

迈不过友情╰ 2022-05-30 07:19 233阅读 0赞

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

题目大意:

代码:

  1. #include<stdio.h>
  2. #include<algorithm>
  3. using namespace std;
  4. int farther[1010],hobby[1010],clusters[1010];
  5. bool cmp(int a,int b)
  6. {
  7. return a>b;
  8. }
  9. int find(int x)
  10. {
  11. while(x!=farther[x])
  12. {
  13. x=farther[x];
  14. }
  15. return x;
  16. }
  17. void Union(int a,int b)
  18. {
  19. int findA=find(a);
  20. int findB=find(b);
  21. if(findA!=findB)
  22. {
  23. farther[findA]=findB;
  24. }
  25. }
  26. int main()
  27. {
  28. int i,j,n,m,k,t,cnt;
  29. scanf("%d",&n);
  30. for(i=1;i<=n;i++)
  31. {
  32. farther[i]=i;
  33. }
  34. for(i=1;i<=n;i++)
  35. {
  36. scanf("%d:",&m);
  37. for(j=0;j<m;j++)
  38. {
  39. scanf("%d",&k);
  40. if(hobby[k]==0)
  41. {
  42. hobby[k]=i;
  43. }
  44. Union(i,find(hobby[k]));
  45. }
  46. }
  47. for(i=1;i<=n;i++)
  48. {
  49. clusters[find(i)]++;
  50. }
  51. cnt=0;
  52. for(i=1;i<=n;i++)
  53. {
  54. if(clusters[i]!=0)
  55. {
  56. cnt++;
  57. }
  58. }
  59. printf("%d\n",cnt);
  60. sort(clusters,clusters+n+1,cmp);
  61. for(i=0;i<cnt;i++)
  62. {
  63. if(i==0)
  64. {
  65. printf("%d",clusters[i]);
  66. }
  67. else
  68. {
  69. printf(" %d",clusters[i]);
  70. }
  71. }
  72. printf("\n");
  73. return 0;
  74. }

发表评论

表情:
评论列表 (有 0 条评论,233人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring social理解

    现在互联网飞速发展,人们每天在互联网上冲浪,获取各种信息。各大网站为了方便用户的登录,提供了各式各样的社交登录,比如:QQ、微信和微博登录等。这些主流的社交登录大多是基于oau

    相关 OPPO.1107刷机笔记

    手动 转移任意APP为系统APP的方法流程简述 宗旨: 保持和系统原本同目录下的文件各种设置(权限,所有者,SE上下文),目录结构保持一致即可! 1. 从`/data

    相关 OPPO.1107刷机笔记

    手动 转移任意APP为系统APP的方法流程简述 宗旨: 保持和系统原本同目录下的文件各种设置(权限,所有者,SE上下文),目录结构保持一致即可! 1. 从 `/dat