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
看着题目就烦,给的数据还是乱七八糟的,看着更烦。
题目大意就是给出 n 个人的信息,一行一个人,
给出该人的id,父亲的id ,母亲的id ,有几个孩子,孩子的id(如果有的话) ,几套房产,房产的面积。
需要注意的是 一:如果父亲或者母亲的 id 是 -1 ,说明没有。
二:如果孩子个数是 0 就不给出孩子的 id 。
题目要求输出的是
1,有多少个家庭
2,输出每个家庭的信息(输出家庭中 id 号最小的人的 id ,该家庭的人口数 , 平均没人几套房(保留3位小数) ,人均面积是多少(保留3位小数) ) 。
输出有多少个家庭,直接查找有多少个根节点即可。
可是输出每个家庭的信息,就比较麻烦了,我们用另外一个数组fam来存储家庭的信息。
题目中说了,输出是有顺序的,人均面积大的先输出,如果相同,id 号小的先输出、这样就涉及到了排序。自定义一下即可。
通过遍历,我们可以把每个fam的 人数 和 总房产套数 ,总面积数 求出来,接下来就可以计算 人均套数 和 面积 。接着 排序。
排序后,输出信息即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10000
using namespace std;
int p[N],vis[N];
int n,k,cnt;
//存储输入数据
struct Data{int id,fa,ma,num,area;int ch[5];
}data[N];
//存储结果
struct Fam{int id,people ;double num,area ;bool flag ;
}fam[N];bool cmp(Fam a ,Fam b){if(a.area != b.area)return a.area > b.area;elsereturn a.id < b.id;
}
//找根节点
int find(int x){return p[x] == x ? x : p[x] = find(p[x]);
}
//合并集合
void join(int x,int y){int fx = find(x);int fy = find(y);if(fx != fy){if(fy<fx) p[fx] = fy;else p[fy] = fx;}
}int main(){//初始化 memset(vis,0,sizeof(vis));for(int i=0 ;i<10000 ;i++){p[i] = i;}//输入数据 scanf("%d",&n);for(int i=0 ;i<n ;i++){scanf("%d%d%d%d",&data[i].id ,&data[i].fa ,&data[i].ma ,&k);vis[data[i].id] = 1;if(data[i].fa!=-1){vis[data[i].fa] = 1;join(data[i].id,data[i].fa);}if(data[i].ma!=-1){vis[data[i].ma] = 1;join(data[i].id,data[i].ma);}if(k){for(int j=0 ;j<k ;j++){scanf("%d",&data[i].ch[j]);vis[data[i].ch[j]] = 1;join(data[i].id,data[i].ch[j]); }}scanf("%d%d",&data[i].num ,&data[i].area);}//处理数据,更新结果数组 for(int i=0 ;i<n ;i++){int id = find(data[i].id);fam[id].id = id;fam[id].num += data[i].num;fam[id].area += data[i].area;fam[id].flag = true;}//计算人数 for(int i=0 ;i<10000 ;i++){if(vis[i])fam[find(i)].people ++;if(fam[i].flag) cnt++;}//计算平均 for(int i=0 ;i<10000 ;i++){if(fam[i].flag){fam[i].num /= fam[i].people;fam[i].area /= fam[i].people;}}sort(fam,fam+10000,cmp);printf("%d\n",cnt);for(int i=0 ;i<cnt ;i++){printf("%04d %d %.3f %.3f\n",fam[i].id,fam[i].people ,fam[i].num ,fam[i].area);}return 0;
}