pat 1107. Social Clusters (30)
来源:互联网 发布:网络作家吧 编辑:程序博客网 时间:2024/05/17 00:10
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:83: 2 7 101: 42: 5 31: 41: 31: 44: 6 8 1 51: 4Sample Output:
34 3 1
vector 的使用+并查集(第一次用vector容器)
#include <iostream>#include <vector>#include <algorithm>#include<string.h>using namespace std;#include<stdio.h>int bin[1005],n;struct node{vector<int> sons;};vector<node> hobby(1001);int findx(int x){ int r=x; while(r!=bin[r]) r=bin[r]; return r;}void fun(int x,int y){ int tx,ty; tx=findx(x); ty=findx(y); if(tx!=ty) bin[tx]=ty;}int main(){ int k,i,j,num,v[1005],ans[1005],sum=0,p[1005]; cin>>n; for(i=0;i<1005;i++) bin[i]=i; for(i=0;i<n;i++) { scanf("%d:",&k); for(j=0;j<k;j++) { cin>>num; hobby[num].sons.push_back(i);//把每个爱好,哪些人共同拥有这些爱好的人的编号集中起来. } } for(i=0;i<=1000;i++) if(hobby[i].sons.size()!=0) { int a=hobby[i].sons[0]; for(j=1;j<hobby[i].sons.size();j++) { int b=hobby[i].sons[j]; fun(a,b); } } memset(ans,0,sizeof(ans)); for(i=0;i<n;i++) v[i]=findx(i); for(i=0;i<n;i++) ans[v[i]]++;//每个集合的人数 sort(ans,ans+1005); int f=0; for(i=1004;i>-1;i--) if(ans[i]!=0) { sum++;//有多少个集合 p[f++]=ans[i]; } cout<<sum<<endl; for(i=0;i<f-1;i++) cout<<p[i]<<" "; cout<<p[i]<<endl; return 0;}
#include<iostream>#include<string.h>#include<stack>#include<vector>using namespace std;#include<stdio.h>#include<algorithm>#include<queue>#include<math.h>#include<map>#define MS(a,b) memset(a,b,sizeof(a))int n,fa[2005];vector<int>hobby[2005];int findx(int x){ int r=x; if(r!=fa[r]) fa[r]=findx(fa[r]); return fa[r];}void Union(int x,int y){ int tx,ty; tx=findx(x); ty=findx(y); if(tx!=ty) fa[tx]=ty;}int main(){ int k,j,i,num,i1,i2,v[2005]; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d: ",&k); for(j=1;j<=k;j++) { scanf("%d",&num); hobby[i].push_back(num); } } for(i=1;i<=n;i++) fa[i]=i; for(i=1;i<n;i++) { for(j=i+1;j<=n;j++) { int sign=0; for(i1=0;i1<hobby[i].size();i1++) { for(i2=0;i2<hobby[j].size();i2++) { if(hobby[i][i1]==hobby[j][i2]) { Union(i,j); sign=1; } } if(sign)break; } } } int ans[2005],ans1[2005],sum=0; MS(ans,0); for(i=1;i<=n;i++) v[i]=findx(i); for(i=1;i<=n;i++) ans[v[i]]++; j=0; for(i=1;i<=n;i++) if(ans[i]!=0) { sum++; ans1[j++]=ans[i]; } sort(ans1,ans1+j); printf("%d\n",sum); for(i=j-1;i>0;i--) printf("%d ",ans1[i]); printf("%d\n",ans1[i]); return 0;}
- 【PAT】1107. Social Clusters (30)
- pat 1107. Social Clusters (30)
- PAT 1107. Social Clusters (30)
- PAT 1107. Social Clusters (30)
- pat 1107. Social Clusters (30)
- PAT 1107. Social Clusters (30)
- 【PAT】1107. Social Clusters
- PAT 1107. Social Clusters
- 1107. Social Clusters (30) PAT甲级
- pat-a 1107. Social Clusters (30)
- PAT A 1107. Social Clusters (30)
- 【PAT】【Advanced Level】1107. Social Clusters (30)
- Pat(A) 1107. Social Clusters (30)
- PAT 甲级 1107. Social Clusters (30)
- PAT A1107. Social Clusters (30)
- PAT A1107 social clusters (30)
- PAT A1107. Social Clusters (30)
- 1107. Social Clusters (30)
- C语言实现大数相加相减和相乘
- TListBox的Item改变高度
- STL_9:关联容器
- iOS开发中输入框的英文和中文的字数限制
- 《挑战程序设计竞赛》2.5.1 最短路 AOJ0189 2249 2200 POJ3255 2139 3259 3268(5)
- pat 1107. Social Clusters (30)
- 本文介绍Android中Intent的各种常见作用
- AndroidStudio快捷键
- 测量宽高祖传代码
- 使用Spring配置实现bean工厂
- Gradle 快速入门
- 百度换肤技术
- 大数据仓库-增量更新
- java.lang.IllegalStateException: No host