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 Child
1
⋯Child
k
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; Child
i
'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 100
Sample Output:
3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000
#include<iostream>
#include<set>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 10000;
int pre[maxn];
struct node {//存储开始输入的人的基本信息
int id, house;
double area;
};
struct family {//输出的家庭结构体
int id, number;
double house, area;
};
bool cmp(family a, family b)//自定义比较函数
{
if (a.area != b.area)return a.area > b.area;
else return a.id < b.id;
}
int find(int x)//并查集模板,查找
{
while (x != pre[x]) x = pre[x];
return x;
}
void merge(int x, int y)//并查集模板,合并
{
int a = find(x);
int b = find(y);
if (a != b) pre[a] = b;
}
int main()
{
int n, x;
cin >> n;
set<int>s;//存储每人的编号,默认升序
map<int, node>nodes;//记录人的房产信息
for (int i = 0; i < maxn; i++)pre[i] = i;//初始化pre数组
for (int i = 0; i < n; i++)
{
int id, p1, p2, k, house, id1;//分别记录编号 父 母 k 孩子1 ... 孩子k 房产套数
double area;// 总面积
cin >> id >> p1 >> p2 >> k;
s.insert(id);//插入id
if (p1 != -1)//有爸爸
{
s.insert(p1);
merge(id, p1);
}
if (p2 != -1)//有妈妈
{
s.insert(p2);
merge(id, p2);
}
for (int j = 0; j < k; j++)//随后的家庭成员
{
cin >> id1;
s.insert(id1);
merge(id1, id);
}
cin >> house >> area;
nodes[id] = node{ id,house,area };//记录该人的家庭信息
}
set<int>s1;//记录家庭编号(也可以理解记录着每个家庭的老大)
map<int, vector<int>>m;//记录家庭编号()和成员编号
for (auto it = s.begin(); it != s.end(); it++)
{
int fa = find(*it);
m[fa].push_back(*it);
s1.insert(fa);
}
cout << s1.size() << endl;//输出家庭的个数
vector<family>v1;
for (auto it = s1.begin(); it != s1.end(); it++)
{
int fa = *it;
double house = 0, area = 0;
for (auto tmp = 0; tmp < m[fa].size(); tmp++)//将每个家庭的成员及其房产计算汇总,放入family结构体中
{
house += nodes[m[fa][tmp]].house;
area += nodes[m[fa][tmp]].area;
}
v1.push_back(family{ m[fa][0],int(m[fa].size()),house * 1.0 / (int)m[fa].size(),area * 1.0 / (int)m[fa].size() });
}
sort(v1.begin(), v1.end(), cmp);//进行排序
for (int i = 0; i < v1.size(); i++) {
printf("%04d %d %.3lf %.3lf", v1[i].id, v1[i].number, v1[i].house, v1[i].area);
if (i < v1.size() - 1) puts("");
}
}