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: 4Sample Output:
3 4 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;
}