1114 Family Property

#include<iostream>
#include<algorithm>
#include<vector>
#include<unordered_set>
using namespace std;
struct node{
	int  totalnum;
	double totoalsets;
	double totoalarea;
	double avgarea;
	double avgset;
}family[10000];
bool visited[10000];
bool rootvisited[10000]={false};
bool appear[10000];
vector<int> father(10000);
vector<int> Root;
unordered_set<int> myset;
int findfather(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 faA=findfather(x);
	int faB=findfather(y);
	if(faA>faB)
		father[faA]=faB;
	else if(faA<faB)
		father[faB]=faA;
}
bool cmp(int root1,int root2){
	if(family[root1].avgarea!=family[root2].avgarea)
		return family[root1].avgarea>family[root2].avgarea;
	else
		return root1<root2;	
}
int main(){
	int N,Person,Father,Mother,child,k;
	double set,area;
	for(int i=0;i<father.size();i++)
		father[i]=i;
	cin>>N;
	for(int i=0;i<N;i++){
		cin>>Person>>Father>>Mother;
		visited[Person]=true;
		if(Father!=-1){
			visited[Father]=true;
			Union(Person,Father);
		}
		if(Mother!=-1){
			visited[Mother]=true;
			Union(Person,Mother);
		}
		cin>>k;
		for(int j=0;j<k;j++){
			cin>>child;
			visited[child]=true;
			Union(Person,child);
		}
		cin>>set>>area;
		family[Person].totoalsets=set;
		family[Person].totoalarea=area;
	}
	
	for(int i=0;i<10000;i++){
		if(visited[i]==true){
			int root=findfather(i);
			if(root==i){
				myset.insert(root);
				family[i].totalnum=1;
				continue;
			}
			
			family[root].totoalarea+=family[i].totoalarea;
			family[root].totoalsets+=family[i].totoalsets;
			family[root].totalnum++; 
			myset.insert(root);
		}
	}
	for(auto st=myset.begin();st!=myset.end();st++){
		Root.push_back(*st);
		int root=*st;
		family[root].avgarea=family[root].totoalarea/family[root].totalnum;
		family[root].avgset=family[root].totoalsets/family[root].totalnum;
	}
	sort(Root.begin(),Root.end(),cmp);
	cout<<myset.size()<<endl;
	for(int i=0;i<Root.size();i++){
		int k=Root[i];
		printf("%04d %d %.3lf %.3lf",k,family[k].totalnum,family[k].avgset,family[k].avgarea);
		if(i!=Root.size()-1)
			cout<<endl;
	} 
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值