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 Mestate 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; Mestate 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
AVGsets AVGarea
where ID
is the smallest ID in the family; M
is the total number of family members; AVGsets is the average number of sets of their real estate; and AVGarea 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 100
Sample Output:
3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000
/**
本题题意 :告诉 总共家庭的 包括 自己的id , 父母的pid 孩子的个数k, 孩子的 k个id 有几栋房子, 房子的总面积,
这些家庭间 有的人id 相同就说明 它们属于一个家庭
最终计算出 所有独立的家庭数, 并输出这些独立家庭中 最小家庭成员id,家庭成员人数,每个平均拥有的房产, 以及房子的平均面积 (保留3位小数)
输出时的顺序是 房子的平均面积 按从大到小, 如果房子平均面积相等 就按照 id 从小到大排列
本题思路:
运用并查集, set[i] 为的下标代表每个人的id (id为4位10进制数构成,因此范围为 10的4次方)
因为最后要排序 设定一个结构体保存 最小的id , 家庭成员人数,房产证数量, 房子面积
1.通过set[i] 能够计算出,每个家庭的人数 (visit[i] == true ) num[find(i)].number[i]++
2.单独开个est数组,are数组 是因为防止 num数组中的estate,area重复多加 est数组能够计算出一个家庭总房产数量
are数组能够计算出一个家庭的总面积 (当 est[i](表示 成员 i所有的房产数量) > 0 , num[find(i)] += est[i] )
(当 art[i](表示 成员 i所有的房产面积) > 0 , num[find(i)] += are[i])
最后 通过 总面积 (总数量) / 总人数(num[i].number) 就为平均数
注意 c++ int 转 double时 必须强制转型;
num[i].avgEstate = double(num[i].estate * 1.0 / num[i].number);
num[i].avgArea = double(num[i].area * 1.0 / num[i].number);
**/
#include<iostream>
#include<algorithm>
using namespace std;
const int maxsize = 1e4 + 5;
struct Node{
int id,number, estate, area; //代表最小的id , 家庭成员人数,房产证数量, 房子面积
double avgEstate, avgArea; //平均房子数量, 平均房子面积
} num[maxsize];
//单独开个est数组,are数组 是因为防止 num数组中的estate,area重复多加
int n, set[maxsize], est[maxsize], are[maxsize];
bool visit[maxsize];
int find(int i){
return set[i] == i? i : set[i] = find(set[i]);
}
void merge(int a, int b){
int FaA = find(a);
int FaB = find(b);
if(FaA != FaB)
set[FaA] = FaB;
}
bool cmp(Node a, Node b){
if(a.avgArea != b.avgArea)
return a.avgArea > b.avgArea;
else
return a.id < b.id;
}
void init(){
for(int i = 0; i < maxsize; i++)
set[i] = i;
}
int main(){
int id, pid, k, childid, estate, area;
scanf("%d", &n);
init();
//开始合并所有人的id
for(int i = 0; i < n; i++){
scanf("%d", &id);
visit[id] = true;
for(int j = 0; j < 2; j++){
scanf("%d", &pid);
visit[pid] = true;
if(pid != -1){
if(find(id) < find(pid)) // 要将最小的点当作最终的家庭集合点, 那么比较大小的也是最终点
merge(pid, id);
else
merge(id, pid);
}
}
scanf("%d", &k);
for(int c = 0; c < k; c++){
scanf("%d", &childid);
visit[childid] = true;
find(childid) < find(id) ? merge(id, childid) : merge(childid, id); //这个判断最好写在merge函数内部,合并时,大的id 连接小的id
}
scanf("%d%d", &estate, &area);
est[find(id)] += estate; //要将 家庭已经有的 面积 加上
are[find(id)] += area;
}
for(int i = 0; i < maxsize; i++){ //开始计算集合(家庭)中(人)结点的数量
if(visit[i] == true){
num[find(i)].id = find(i);
num[find(i)].number++;
}
}
int sum = 0;//计算家庭的数量 ,计算每个最终结点(集合(家庭))的estate,area的数量和
for(int i = 0; i < maxsize; i++){
if(num[i].number != 0)
sum++;
if(est[i] != 0 && are[i] != 0){
num[find(i)].estate += est[i];
num[find(i)].area += are[i];
}
}
for(int i = 0; i < maxsize; i++){
if(num[i].estate > 0 && num[i].area > 0) {
num[i].avgEstate = double(num[i].estate * 1.0 / num[i].number);
num[i].avgArea = double(num[i].area * 1.0 / num[i].number);
}
}
sort(num, num + maxsize, cmp);
printf("%d\n", sum);
for(int i = 0; i < sum; i++){
printf("%04d %d %.3lf %.3lf\n", num[i].id, num[i].number, num[i].avgEstate, num[i].avgArea);
}
return 0;
}