# PAT-天梯赛-L2.007家庭房产-未解决（最后一个测试点）

#include<algorithm>
#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using namespace std;
const int Size=10000;// 不用Map实现 数组占用内存太多了···
int Par[Size];
int RenKouShu[Size];
int FangChanZongShu[Size];// 家庭i的房产总数
double FangChanZongMianJi[Size];// 家庭i的房产总面积
double RenJunFangchanTaoShu[Size];
double RenJunFangchanMianJi[Size];

struct Node
{
int BianHao;
int JiaTingRenKouShu;
double RenJunFangChanTaoShu;
double RenJunFangChanMianJi;
};

bool Cmp(const Node &a, const Node &b)
{
if(a.RenJunFangChanMianJi!=b.RenJunFangChanMianJi)
return a.RenJunFangChanMianJi>b.RenJunFangChanMianJi;
return a.BianHao<b.BianHao;
}

int Get_Par(int a)
{
if(a!=Par[a])
Par[a]=Get_Par(Par[a]);
return Par[a];
}

void Merge(int a, int b)
{
//需要判断各自的祖先节点 编号小的那个为最终祖先 因为最后输出要求
int ParA=Get_Par(a);
int ParB=Get_Par(b);
if(ParA<ParB)
{
//cout<<"This Merge the"<<ParB<<"Big than"<<ParA<<" "<<endl;
Par[ParB]=ParA;
//cout<<"And the Par of "<<ParB<<"is "<<ParA<<endl;
RenKouShu[ParA]+=RenKouShu[ParB];
FangChanZongShu[ParA]+=FangChanZongShu[ParB];
FangChanZongMianJi[ParA]+=FangChanZongMianJi[ParB];
}
else if(ParA>ParB)
{
//cout<<"This Merge the"<<ParA<<"Big than"<<ParB<<" "<<endl;

Par[ParA]=ParB;
//cout<<"And the Par of "<<ParA<<"is "<<ParB<<endl;
RenKouShu[ParB]+=RenKouShu[ParA];
FangChanZongShu[ParB]+=FangChanZongShu[ParA];
FangChanZongMianJi[ParB]+=FangChanZongMianJi[ParA];
}
}

int  main()
{
//ifstream cin("SARS.txt");
int N;
cin>>N;
int BianHao,Fu,Mu,k,Child,FangChanShu;
double ZongMianji;
memset(Par,0,sizeof(Par));
memset(FangChanZongShu,0,sizeof(FangChanZongShu));
memset(FangChanZongMianJi,0,sizeof(FangChanZongMianJi));

for(int i=0; i<N; i++)
{
cin>>BianHao>>Fu>>Mu;
if(Par[BianHao]==0){
RenKouShu[BianHao]=1;
Par[BianHao]=BianHao;
}
if(Fu!=-1)
{
if(Par[Fu]==0){
RenKouShu[Fu]=1;
Par[Fu]=Fu;
}
Merge(BianHao,Fu);
}
if(Mu!=-1)
{
if(Par[Mu]==0){
RenKouShu[Mu]=1;
Par[Mu]=Mu;
}
Merge(BianHao,Mu);
}
cin>>k;
while(k--)
{
cin>>Child;
if(Par[Child]==0){
RenKouShu[Child]=1;
Par[Child]=Child;
}
Merge(BianHao,Child);
}
cin>>FangChanShu;
FangChanZongShu[Get_Par(BianHao)]+=FangChanShu;
cin>>ZongMianji;
FangChanZongMianJi[Get_Par(BianHao)]+=ZongMianji;
}
int ans=0;
vector<Node>Vec;
Node node;
for(int i=0;i<10000; i++)
{
if(Par[i]==i&&RenKouShu[i]!=0){
ans++;
node.BianHao=i;
node.JiaTingRenKouShu=RenKouShu[i];
node.RenJunFangChanTaoShu = FangChanZongShu[i]*1.0/RenKouShu[i];
node.RenJunFangChanMianJi = FangChanZongMianJi[i]/RenKouShu[i];
Vec.push_back(node);
}
}
cout<<ans<<endl;
sort(Vec.begin(),Vec.end(),Cmp);
for(int i=0; i<Vec.size();i++)
{
printf("%04d %d %.3f %.3f\n",Vec[i].BianHao,Vec[i].JiaTingRenKouShu,Vec[i].RenJunFangChanTaoShu,Vec[i].RenJunFangChanMianJi);
}
return 0;
}



#### PATL2-007. 家庭房产详解(并查集)

2016-07-13 22:01:55

#### PATL2-007. 家庭房产-并查集

2017-03-07 23:34:27

#### ACM天梯赛练习L1-016. 查验身份证

2016-06-05 16:57:04

#### 天梯赛模拟题- N个数求和

2017-02-21 19:47:25

#### L1-016. 查验身份证-PAT团体程序设计天梯赛GPLT

2016-07-21 23:00:41

#### L1-009. N个数求和-PAT团体程序设计天梯赛GPLT

2016-08-06 04:28:25

#### L1-006. 连续因子-PAT团体程序设计天梯赛

2016-07-21 23:26:34

#### ACM天梯赛练习L1-005. 考试座位号

2016-06-05 15:23:02

#### PAT 团体程序设计天梯赛 部分题目题解

2016-07-12 11:18:17

#### PAT 团体程序设计天梯赛-练习集 题解（凑零钱,堆栈,社交集群）

2016-04-16 22:06:32