【并查集】PAT A1107 Social Clusters (30分)

疑问

暂无

代码

#include<cstdio> 
#include<algorithm>

using namespace std;

const int maxn = 1010;	//从1开始编号

int father[maxn];

int hashTable[maxn];	//存储这个簇有多少人 

void init(){
	for(int i=1;i<=maxn;i++){
		father[i] = i;
	}
}

//采用递归实现 
int findFather(int x){
	if(x == father[x]){
		return x;
	}else{
		int F = findFather(father[x]);
		father[x] = F;
		return F;
	}
}

//合并
void Union(int a,int b){
	int faA = findFather(a);
	int faB = findFather(b); 
	
	if(faA != faB){	//两者的父亲不一样 
		father[faB] = faA;	//将faB代表的簇合并到faA中 
		hashTable[faA] += hashTable[faB];	//将以faB为根结点的簇的人数转移到以faA为根结点的簇 
		hashTable[faB] = 0;	//设置簇的成员数为0 
	}
} 

bool cmp(int a,int b){
	return a>b;
}

int main(){
	fill(hashTable,hashTable+maxn,0);
	
	int n;
	scanf("%d",&n);
	
	init();
	
	
	
	for(int i=0;i<n;i++){
		int m;
		scanf("%d:",&m);
		int cur; 
		int curF;	//当前读入的结点簇的根结点
		scanf("%d",&cur);
		curF = findFather(cur);	//找到该兴趣所在的簇 
		hashTable[curF]++;
		for(int j=1;j<m;j++){
			scanf("%d",&cur);
			Union(curF,cur);
		} 
	}
	
	sort(hashTable,hashTable+maxn,cmp);
	
	int count = 0;
	
	for(int i=0;i<maxn;i++){
		if(hashTable[i] !=0 ){
			count++;
		}else{
			break;
		}
	}
	
	printf("%d\n",count);
	
	for(int i=0;i<count;i++){
		printf("%d%c",hashTable[i],hashTable[i+1]!=0?' ':NULL);
	}
	
	return 0;
} 

 

反思

  1. 我这里的并查集里面的元素是爱好,关联的爱好放到一个簇里面。与《算法笔记》上的解题思路不太一样;
  2. 第一次提交没有过题,因为自己粗心把init()里面的边界值写成了n,而不是maxn

二刷代码

二刷依然是把hobby看作并查集的元素

在这里插入代码片//对hobby进行并查集操作! 
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 1010;

int father[maxn];//代表i的父亲 
map<int,int> mp;//代表以i为根节点的集合大小 
int n;

int findFather(int x){
	if(father[x] == x){//根节点 
		return x;
	}else{
		int F = findFather(father[x]);
		father[x] = F;
		return F;
	}
} 

void Union(int u,int v){
	int fatherU = findFather(u);
	int fatherV = findFather(v);
	
	//比较父亲
	if(fatherU != fatherV){
		//首先将人数移过去
		mp[fatherU] = mp[fatherU]+mp[fatherV];
		mp.erase(fatherV);	//移除,因为合并了 
		father[fatherV] = fatherU;
	}
} 

int main(){
	for(int i=0;i<maxn;i++){
		father[i]=i;//初始化 
	}
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int k;
		scanf("%d: ",&k);
		int base;
		scanf("%d",&base);
		for(int i=0;i<k-1;i++){
			int cur;
			scanf("%d",&cur);
			Union(base,cur);
		}
		//合并之后再来加入一个新的元素
		mp[findFather(base)] += 1;
	}
	
	//接下来进行输出试试
	printf("%d\n",mp.size()); 
	//遍历结果再排序
	vector<int> res;
	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
		res.push_back(it->second);
	}
	sort(res.begin(),res.end());
	for(int i=0;i<res.size();i++){
		printf("%d",res[res.size()-1-i]);
		if(i!=res.size()-1){
			printf(" ");
		}
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值