PAT | A1114 Family Property

并查集

#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

struct Family{
	int numOfMembers;
	int numOfHouses;
	int totalArea;
	int minId;
};

bool cmp(Family a,Family b){
	double avera = (double)a.totalArea / a.numOfMembers,averb = (double)b.totalArea / b.numOfMembers;
	if(avera != averb)
		return avera > averb;
	else
		return a.minId < b.minId;
}

unordered_map<int,int> idInVec;
unordered_map<int,Family> rootInFamily;
vector<int> pre;
vector<Family> res;

int unionSearch(int root){
	int son,tmp;
	son = root;
	while(root != pre[root])
		root = pre[root];
	while(son != root){
		tmp = pre[son];
		pre[son] = root;
		son = tmp;
	}
	return root;
}

void join(int root1,int root2){
	int x,y;
	x = unionSearch(root1);
	y = unionSearch(root2);
	if(x != y){
		pre[x] = y;
		rootInFamily[y].minId = min(rootInFamily[y].minId,rootInFamily[x].minId);
		rootInFamily[y].numOfHouses += rootInFamily[x].numOfHouses;
		rootInFamily[y].numOfMembers += rootInFamily[x].numOfMembers;
		rootInFamily[y].totalArea += rootInFamily[x].totalArea;
		rootInFamily.erase(rootInFamily.find(x));
	}
}

int main(){
	int n;
	scanf("%d",&n);
	for(int i = 0;i < n;i++){
		Family temp;
		temp.numOfHouses = 0;
		int id,father,mother;
		scanf("%d %d %d",&id,&father,&mother);
		if(idInVec.find(id) == idInVec.end()){
			pre.push_back(pre.size());
			idInVec[id] = pre.size() - 1;
			rootInFamily[idInVec[id]] = temp;
			rootInFamily[idInVec[id]].minId = id;
			rootInFamily[idInVec[id]].numOfHouses = 0;
			rootInFamily[idInVec[id]].numOfMembers = 1;
			rootInFamily[idInVec[id]].totalArea = 0;
		}
		if(father != -1){
			if(idInVec.find(father) == idInVec.end()){ // 父亲是第一次被发现
				pre.push_back(pre.size());
				idInVec[father] = pre.size() - 1;
				rootInFamily[idInVec[father]] = temp;
				rootInFamily[idInVec[father]].minId = father;
				rootInFamily[idInVec[father]].numOfHouses = 0;
				rootInFamily[idInVec[father]].numOfMembers = 1;
				rootInFamily[idInVec[father]].totalArea = 0;
				join(pre[idInVec[id]],pre[idInVec[father]]);
			}else{
				join(pre[idInVec[id]],pre[idInVec[father]]);
			}
		}
		if(mother != -1){
			if(idInVec.find(mother) == idInVec.end()){ // 父亲是第一次被发现
				pre.push_back(pre.size());
				idInVec[mother] = pre.size() - 1;
				rootInFamily[idInVec[mother]] = temp;
				rootInFamily[idInVec[mother]].minId = mother;
				rootInFamily[idInVec[mother]].numOfHouses = 0;
				rootInFamily[idInVec[mother]].numOfMembers = 1;
				rootInFamily[idInVec[mother]].totalArea = 0;
				join(pre[idInVec[id]],pre[idInVec[mother]]);
			}else{
				join(pre[idInVec[id]],pre[idInVec[mother]]);
			}
		}
		int k;
		scanf("%d",&k);
		while(k > 0){
			int child;
			scanf("%d",&child);
			if(idInVec.find(child) == idInVec.end()){ // 父亲是第一次被发现
				pre.push_back(pre.size());
				idInVec[child] = pre.size() - 1;
				rootInFamily[idInVec[child]] = temp;
				rootInFamily[idInVec[child]].minId = child;
				rootInFamily[idInVec[child]].numOfHouses = 0;
				rootInFamily[idInVec[child]].numOfMembers = 1;
				rootInFamily[idInVec[child]].totalArea = 0;
				join(pre[idInVec[id]],pre[idInVec[child]]);
			}else{
				join(pre[idInVec[id]],pre[idInVec[child]]);
			}
			k--;
		}
		int houseNum,houseArea;
		scanf("%d %d",&houseNum,&houseArea);
		rootInFamily[unionSearch(idInVec[id])].numOfHouses += houseNum;
		rootInFamily[unionSearch(idInVec[id])].totalArea += houseArea;
	}
	int s = rootInFamily.size();
	printf("%d\n",s);
	unordered_map<int,Family>::iterator it = rootInFamily.begin();
	while(it != rootInFamily.end()){
		res.push_back(it->second);
		it++;
	}
	sort(res.begin(),res.end(),cmp);
	for(int i = 0;i < res.size();i++){
		printf("%04d %d %.3f %.3f",res[i].minId,res[i].numOfMembers,(double)res[i].numOfHouses / res[i].numOfMembers,(double)res[i].totalArea / res[i].numOfMembers);
		if(i != res.size() - 1)
			printf("\n");
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值