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 <cstdio>
#include <queue>
#include <cmath>
#include <iostream>
using namespace std;
int f[10000];//父亲
int book[10000];//该编号出现否
int count[10000];//几套房子
int son[10000];//父亲节点的 家庭成员数
int area[10000];//房子面积
int Min[10000];//家庭成员最小编号
struct node{//用于最后结果的排序
int minNum;
int sons;
double averSet;
double averArea;
};
void Init(){
for(int i=0;i<=9999;i++){
f[i]=i;
book[i]=0;
count[i]=0;
area[i]=0;
Min[i]=i;
son[i]=1;
}
}
int Get(int a){
return f[a]=(f[a]==a?a:Get(f[a]));
}
void Merge(int a,int b){
int x=Get(a);
int y=Get(b);
if(x!=y){
f[y]=x;
}
}
struct cmp{//优先队列的重载仿函数
bool operator() (node a,node b){
if(fabs(a.averArea-b.averArea)>1e-6){
return a.averArea<b.averArea;
}
else{
return a.minNum>b.minNum;
}
}
};
int main(){
int n,set=0;//set为不相交集数目
int num,fa,ma,child,temp;
priority_queue<node,vector<node>,cmp> q;
struct node a;
Init();
cin>>n;
for(int i=1;i<=n;i++){//处理信息
cin>>num;
cin>>fa>>ma;
book[num]=1;
if(fa!=-1){
book[fa]=1;
Merge(fa,num);
if(ma!=-1){
book[ma]=1;
Merge(fa,ma);
}
}
else if(ma!=-1){
book[ma]=1;
Merge(ma,num);
}
cin>>child;
for(int j=1;j<=child;j++){
cin>>temp;
book[temp]=1;
Merge(num,temp);
}
cin>>count[num]>>area[num];
}
for(int i=0;i<=9999;i++){//找不相交集并更新信息
if(book[i]){
int f=Get(i);
if(f==i){
set++;
continue;
}
Min[f]=min(Min[f],i);
son[f]++;
count[f]+=count[i];
area[f]+=area[i];
book[i]=0;
}
}
cout<<set<<endl;
for(int i=0;i<=9999;i++){//将不相交集入队
if(book[i]){
a.minNum=Min[i];
a.sons=son[i];
a.averSet=(double)count[i]/son[i];
a.averArea=(double)area[i]/son[i];
q.push(a);
}
}
while(!q.empty()){
struct node b=q.top();
q.pop();
printf("%04d %d %.3f %.3f\n",b.minNum,b.sons,b.averSet,b.averArea);
}
return 0;
}
//此题也可以用搜索做,寻找每个连通分量的相关信息