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
题目大意:有N个人(编号1~N表示)每一个人 Ki有K个爱好分别为 hi[1] hi[2] ... hi[Ki]。具有相同爱好的人属于一个圈子。求出圈子个数,并按圈子人数大小进行降序输出。
分析:采用并查集可以实现相同爱好人的并、查操作。
维基百科链接:并查集
回顾并查集操作:
1 初始化
一开始,每个元素都是独立的集合,因此另所有的father[i]=i;
for(int i=1;i<=n;i++){
father[i]=i;
}
2 查找
反复查找父亲结点,直到找到根节点( father[i]==i )
int Findfather(int x){
while(x!=father[x]){
x=father[x];
}
return x;
}
3 合并
(1)对于给定的元素a,b,判断他们是否属于同一集合,调用查找函数即可
(2)合并两个集合,(1)中已经获得两个元素的根节点Fa和Fb,因此只需要将其中一个的父亲节点指向另一个节点即可。
father[Fa]=Fb / father[Fb]=Fa 都行
代码:
void Union(int a,int b){
int Fa=Findfather(a);
int Fb=Finafather(b);
if(Fa!=Fb) father[Aa]=Fb;
}
4 路径压缩:
上面的查找函数没有优化,极端情况下查找效率比较低(比如说,退化为一条链),而我们“查”操作只是为了找到某一结点的根节点,因此可以想办法进行等价转换。自然而然就想到: 把当前查询结点的路径上的所有结点的父亲结点都指向根节点。
转化过程分为两步:
(1)找x的根节点 r
(2)重新从x开始走一遍寻找根节点过程,把路径上的所有结点的父亲结点全部改为根节点r
int Findfather(int x){
int a=x;
while(x!=father[x]){ //寻找根节点
x=father[x];
}
//到这里x存放的是根节点。下面吧路径上所有节点的father改成根结点
while(a!=father[a]){
int z=a;
father[z]=x;
a=father[a]; //a回溯父节点
}
return x;
}
有了上面的并查集基础,这道题就很简单了。这道题只是把结点存在爱好course[ ] 数组中,因此对course[ ] 进行并查操作即可。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int father[N],root[N],course[N];
int find(int x){
int a=x;
while(x!=father[x]){
x=father[x];
}
//压缩路径
while(a!=father[a]){
int z=a;
a=father[a];//向前索引查找
father[z]=x;
}
return x;
}
void union_(int x,int y){
int fx=find(x);
int fy=find(y);
if(fx!=fy){
father[fx]=fy;//fx合并到以fy为根集合中
}
}
bool cmp(int a,int b){
return a>b;
}
int main(){
int n,k,index;
cin>>n;
for(int i=1;i<=n;i++){
father[i]=i;// 初始化为独立的节点
root[i]=0;
}
for(int i=1;i<=n;i++){
scanf("%d:",&k);
for(int j=1;j<=k;j++){
scanf("%d",&index);
if(!course[index]){
course[index]=i;//index爱好指向 i
}
union_(i,find(course[index]));
}
}
for(int i=1;i<=n;i++){
root[find(i)]++;//i的根节点是find(i),人数 ++
}
int ans=0;
for(int i=1;i<=n;i++){
if(root[i]) ans++;
}
cout<<ans<<endl;
sort(root+1,root+1+n,cmp);
cout<<root[1];
for(int i=2;i<=ans;i++)
cout<<" "<<root[i];
cout<<endl;
return 0;
}