1114. Family Property (25)
This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=1000). Then N lines follow, each gives the infomation of a person who owns estate in the format:
ID Father Mother k Child1 ... Childk M_estate Area
where ID is a unique 4-digit identification number for each person; Father and Mother are the ID's of this person's parents (if a parent has passed away, -1 will be given instead); k (0<=k<=5) is the number of children of this person; Childi's are the ID's of his/her children;M_estate is the total number of sets of the real estate under his/her name; and Area is the total area of his/her estate.
Output Specification:
For each case, first print in a line the number of families (all the people that are related directly or indirectly are considered in the same family). Then output the family info in the format:
ID M AVG_sets AVG_area
where ID is the smallest ID in the family; M is the total number of family members; AVG_sets is the average number of sets of their real estate; and AVG_area is the average area. The average numbers must be accurate up to 3 decimal places. The families must be given in descending order of their average areas, and in ascending order of the ID's if there is a tie.
Sample Input:10 6666 5551 5552 1 7777 1 100 1234 5678 9012 1 0002 2 300 8888 -1 -1 0 1 1000 2468 0001 0004 1 2222 1 500 7777 6666 -1 0 2 300 3721 -1 -1 1 2333 2 150 9012 -1 -1 3 1236 1235 1234 1 100 1235 5678 9012 0 1 50 2222 1236 2468 2 6661 6662 1 300 2333 -1 3721 3 6661 6662 6663 1 100Sample Output:
3 8888 1 1.000 1000.000 0001 15 0.600 100.000 5551 4 0.750 100.000
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define N 10005
int length;
struct person{
int bj;
int id;
int father,mother;
vector<int> sons;
int estate;
int noe;
person(){
bj=-1;
estate=0;
noe=0;
}
}p[N];
struct family{
int smallest,people;
double aoe,aoa,suma,sumn;
family(){
smallest=N;
people=0;
aoe=0;
suma=0;
sumn=0;
aoa=0;
}
void cal(){
aoe=sumn/people;
aoa=suma/people;
}
};
int vis[N]={0};
vector<family> ans;
void BL(int index){
queue<person> q;
q.push(p[index]);
family f=family();
while(!q.empty()){
person top=q.front();
q.pop();
if(vis[top.id]==1)continue;
vis[top.id]=1;
f.suma+=top.estate;
f.people++;
if(top.id<f.smallest)f.smallest=top.id;
f.sumn+=top.noe;
if(top.father!=-1)q.push(p[top.father]);
if(top.mother!=-1)q.push(p[top.mother]);
for(int i=0;i<top.sons.size();i++){
q.push(p[top.sons[i]]);
}
}
f.cal();
ans.push_back(f);
}
bool cmp(family a,family b){
if(a.aoa!=b.aoa)return a.aoa>b.aoa;
else return a.smallest<b.smallest;
}
int main(){
//freopen("test.txt","r",stdin);
cin>>length;
int i;
int ls1,ls2,ls3,ls4,ls5,ls6;
for(i=0;i<N;i++){
p[i].id=i;
p[i].father=-1;
p[i].mother=-1;
}
for(i=0;i<length;i++){
scanf("%d%d%d%d",&ls1,&ls2,&ls3,&ls4);
p[ls1].bj=1;
p[ls1].id=ls1;
p[ls1].father=ls2;
p[ls1].mother=ls3;
if(ls2!=-1)p[p[ls1].father].sons.push_back(ls1);
if(ls3!=-1)p[p[ls1].mother].sons.push_back(ls1);
for(int j=0;j<ls4;j++){
int ls;
scanf("%d",&ls);
p[ls1].sons.push_back(ls);
if(p[ls].father==ls1||p[ls].mother==ls1)continue;
else {
if(p[ls].father==-1)p[ls].father=ls1;
else p[ls].mother=ls1;
}
}
scanf("%d%d",&ls5,&ls6);
p[ls1].noe=ls5;
p[ls1].estate=ls6;
}
for(i=0;i<N;i++){
if(p[i].bj==1&&vis[i]==0){
BL(i);
}
}
cout<<ans.size()<<endl;
sort(ans.begin(),ans.end(),cmp);
for(i=0;i<ans.size();i++){
printf("%04d %d %.3lf %.3lf\n",ans[i].smallest,ans[i].people,ans[i].aoe,ans[i].aoa);
}
return 0;
}