1107 Social Clusters (30 point(s))

本文介绍了一种使用并查集算法解决社交网络中寻找具有相同兴趣爱好的人群集群问题的方法。通过将具有相同爱好的人归类到同一个集合中,算法能够高效地找出社交网络中存在的所有集群,并统计每个集群的人数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

K​i​​: h​i​​[1] h​i​​[2] ... h​i​​[K​i​​]

where K​i​​ (>0) is the number of hobbies, and h​i​​[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表示)每一个人 K​i有K个爱好分别为 h​i​​[1] h​i​​[2] ... h​i​​[K​i​​]。具有相同爱好的人属于一个圈子。求出圈子个数,并按圈子人数大小进行降序输出。

分析:采用并查集可以实现相同爱好人的并、查操作。

维基百科链接:并查集

回顾并查集操作:

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;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZCAIHUI_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值