1107. Social Clusters (30)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1107. Social Clusters (30)相关的知识,希望对你有一定的参考价值。
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:
8 3: 2 7 10 1: 4 2: 5 3 1: 4 1: 3 1: 4 4: 6 8 1 5 1: 4
Sample Output:
3 4 3 1
1 #include<stdio.h> 2 #include<vector> 3 #include<algorithm> 4 #include<math.h> 5 using namespace std; 6 int ans[111][111]; 7 8 bool cmp(int a,int b) 9 { 10 return a > b; 11 } 12 13 struct node 14 { 15 vector<int> child; 16 }; 17 18 node Tree[100100]; 19 20 int MIN = 100100; 21 int cnt = 0; 22 bool vis[1100]; 23 vector<int> Fun[1100]; 24 vector<int> stu[1100]; 25 void DFS(int root,int& sum) 26 { 27 for(int i = 0 ;i < stu[root].size();++i) 28 { 29 for(int k = 0 ;k < Fun[ stu[root][i] ].size();++k) 30 { 31 if(!vis[Fun[ stu[root][i] ][k]]) 32 { 33 ++sum; 34 vis[Fun[ stu[root][i] ][k]] = 1; 35 DFS(Fun[ stu[root][i] ][k],sum); 36 } 37 } 38 } 39 } 40 41 42 int main() 43 { 44 int n,num,tem; 45 double pri,rate; 46 scanf("%d",&n); 47 for(int i = 1 ;i <= n ;++i) 48 { 49 scanf("%d",&num); 50 getchar(); 51 for(int k = 0 ;k < num;++k) 52 { 53 scanf("%d",&tem); 54 Fun[tem].push_back(i); 55 stu[i].push_back(tem); 56 } 57 } 58 vector<int> vv; 59 int cnt = 0;; 60 for(int i = 1;i <= n ;++i) 61 { 62 if(!vis[i]) 63 { 64 int sum = 0; 65 ++cnt; 66 DFS(i,sum); 67 vv.push_back(sum); 68 } 69 } 70 sort(vv.begin(),vv.end(),cmp); 71 printf("%d\n",vv.size()); 72 for(int i = 0 ;i < vv.size();++i) 73 { 74 if(i == 0) printf("%d",vv[0]); 75 else printf(" %d",vv[i]); 76 } 77 printf("\n"); 78 return 0; 79 }
以上是关于1107. Social Clusters (30)的主要内容,如果未能解决你的问题,请参考以下文章
PAT Advanced 1107 Social Clusters (30) [并查集]
骞舵煡闆嗏€斺€擜1107.Social Clusters(30)