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<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<string>
using namespace std;
const int Max = 10001;
struct Person
{
int Name;
int Father,Mother;
vector<int> child;
double sets;
double area;
bool visit;
}S[Max];
struct Family
{
int Id;
double area;
double sets;
int num;
}F[Max];
void DFS(int v,int k)
{
if(v==-1) return ;
F[k].num++;
if(v<F[k].Id) F[k].Id=v;
F[k].area+=S[v].area;
F[k].sets+=S[v].sets;
S[v].visit=true;
if(S[S[v].Father].visit==false)
{
DFS(S[v].Father,k);
}
if(S[v].Mother!=-1&&S[S[v].Mother].visit==false)
{
DFS(S[v].Mother,k);
}
for(int i=0;i<S[v].child.size();i++)
{
int kk=S[v].child[i];
if(S[kk].visit==false)
{
DFS(kk,k);
}
}
}
int Pe[1001],pe=0;
bool cmp(Family a , Family b)
{
if(a.area!=b.area) return a.area>b.area;
else return a.Id<b.Id;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<Max;i++)
{
S[i].area=0;
S[i].Father=-1;
S[i].Mother=-1;
S[i].sets=0;
S[i].visit=false;
F[i].area=0;
F[i].Id=11111;
F[i].num=0;
F[i].sets=0;
}
for(int i=0;i<n;i++)
{
int Id,Fa,Ma,ch;
double are,set;
scanf("%d%d%d%d",&Id,&Fa,&Ma,&ch);
Pe[i]=Id;
for(int i=0;i<ch;i++)
{
int temp;
scanf("%d",&temp);
S[Id].child.push_back(temp);
}
scanf("%lf%lf",&set,&are);
S[Id].Father=Fa;
S[Fa].child.push_back(Id);
S[Id].Mother=Ma;
S[Ma].child.push_back(Id);
S[Id].sets=set;
S[Id].area=are;
}
int Fm=0;
for(int i=0;i<n;i++)
{
int Id=Pe[i];
if(S[Id].visit==false)
{
DFS(Id,Fm);
F[Fm].area/=F[Fm].num;
F[Fm].sets/=F[Fm].num;
Fm++;
}
}
sort(F,F+Fm,cmp);
printf("%d\n",Fm);
for(int i=0;i<Fm;i++)
{
printf("%04d %d %.3f %.3f\n",F[i].Id,F[i].num,F[i].sets/F[i].num,F[i].area/F[i].num);
}
system("pause");
return 0;
}