标签:== cst and common def cas href positive insert
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.
Each input file contains one test case. For each test case, the first line contains a positive integer N (≤), 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:
K?i??: h?i??[1] h?i??[2] ... h?i??[K?i??]
where K?i?? (>) is the number of hobbies, and [ is the index of the j-th hobby, which is an integer in [1, 1000].
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.
8 3: 2 7 10 1: 4 2: 5 3 1: 4 1: 3 1: 4 4: 6 8 1 5 1: 4
3 4 3 1
1 /* 2 time: 2019-06-23 14:07:12 3 problem: PAT_A1107#Social Clusters 4 AC: 34:25 5 6 题目大意: 7 把一群具有相同爱好的人归为一个社交圈,找出所有的社交圈 8 输入: 9 第一行给出,总人数N<=1e3,编号从1~N 10 接下来N行,给出第i个人的,爱好总数K,各个爱好 11 输出: 12 第一行给出,社交圈总数 13 第二行给出,各个社交圈的人数,从多到少 14 15 基本思路: 16 基于兴趣做并查集操作, 17 输入每个人的兴趣,首个兴趣的Hash值+1,标记人数 18 统计父结点个数及其孩子的哈希值即可 19 */ 20 #include<cstdio> 21 #include<set> 22 #include<algorithm> 23 using namespace std; 24 const int M=1e3+10; 25 int fa[M],man[M]={0},ans[M]={0}; 26 27 int Father(int v) 28 { 29 int x=v,s; 30 while(fa[v] != v) 31 v = fa[v]; 32 while(fa[x] != x){ 33 s = fa[x]; 34 fa[x] = v; 35 x = s; 36 } 37 return v; 38 } 39 40 void Union(int v1, int v2) 41 { 42 int f1 = Father(v1); 43 int f2 = Father(v2); 44 fa[f2] = f1; 45 Father(v2); 46 } 47 48 int main() 49 { 50 #ifdef ONLINE_JUDGE 51 #else 52 freopen("Test.txt", "r", stdin); 53 #endif // ONLINE_JUDGE 54 55 for(int i=0; i<M; i++) 56 fa[i]=i; 57 58 int n,m,h1,h2; 59 set<int> hobby,clster; 60 scanf("%d", &n); 61 for(int i=0; i<n; i++) 62 { 63 scanf("%d:%d", &m,&h1); 64 man[h1]++; 65 hobby.insert(h1); 66 for(int j=1; j<m; j++) 67 { 68 scanf("%d", &h2); 69 hobby.insert(h2); 70 Union(h1,h2); 71 h1=h2; 72 } 73 } 74 for(auto it=hobby.begin(); it!=hobby.end(); it++){ 75 ans[Father(*it)] += man[*it]; 76 clster.insert(Father(*it)); 77 } 78 printf("%d\n", clster.size()); 79 sort(ans, ans+M, greater<int>() ); 80 for(int i=0; i<clster.size(); i++) 81 printf("%d%c", ans[i], i+1==clster.size()?‘\n‘:‘ ‘); 82 83 return 0; 84 }
标签:== cst and common def cas href positive insert
原文地址:https://www.cnblogs.com/blue-lin/p/11072913.html