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
输入:第一行输入一个小于等于1000的整数n,在接下来的n行中,依次输入id,父亲的id,母亲的id(如果父母有亡故就输入-1),然后是孩子的个数k,以及k个孩子的id,最后输入该id拥有的房地产数量,房地产面积。
题目的要求是 首先要统计出有几个家族,接下来要按照人均房地产面积从大到小的顺序来进行排序,分别输出家庭成员中编号最小的id,家庭成员个数,家庭成员人均拥有的房子数量,以及人均房地产面积,如果人均房地产面积相同就按照id从小到大的顺序输出。
①输入成员的时候,如果有上下代关系,先用边来存储这个关系。并且用两个数组分别存储每个id拥有的房地产数目以及房地产面积。
②将每一条边的关系都转入并查集,因为最后要输出每个家族中id最小的成员,所以在合并之前都要先判断一下哪个结点的id小,将小的id作为根节点,并且将id大的房地产全部归到id小的那个名下,人口也是。
③遍历并查集,把每一个根节点的信息(id,家族人口数,家族拥有的房地产数量,面积,平均数量,面积)存入结构体中
④对结构体里面的数据按照题意进行排序
⑤输出结果
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 10010;
//he代表每人拥有的房产套数,ha代表每人拥有的房产面积,st表示该id是否出现过,num代表该家族人数
int n, p[N], he[N], ha[N], num[N];
bool st[N];
//e用来表示孩子与父母亲之间的关系,若两个id之间有关系则边存在
struct edge{
int a,b;
}e[N];
struct family{
int id, num, estate, area;
float avg_e, avg_area;
bool operator< (const family & t)const
{
if (avg_area != t.avg_area)
return avg_area > t.avg_area;
else
return id < t.id;
}
};
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
cin >> n;
int m = 0;
for(int i = 0; i < n; i ++ )
{
int id, father, mother, k;
cin >> id >> father >> mother >> k;
st[id] = true;
if(father != -1) e[m ++ ] = {id, father}, st[father] = true;
if(mother != -1) e[m ++ ] = {id, mother}, st[mother] = true;
for(int j = 0; j < k; j ++ )
{
int child;
cin >> child;
st[child] = true;
e[m ++ ] = {id, child};
}
int estate, area;
cin >> estate >> area;
he[id] = estate, ha[id] = area;
}
//初始化并查集
for(int i = 0; i < N ; i ++ ) p[i] = i, num[i] = 1;
for(int i = 0; i < m; i ++ )
{
int a = e[i].a, b = e[i].b;
int pa = find(a), pb = find(b);
if(pa != pb)
{
//保证一个家族的根节点是id最小的那个人
if(pa > pb) swap(pa, pb);
num[pa] += num[pb];
he[pa] += he[pb];
ha[pa] += ha[pb];
p[pb] = pa;
}
}
vector<family>f;
int all = 0;
for(int i = 0; i < N; i ++ )
if(st[i] && p[i] == i)
{
f.push_back({i, num[i], he[i], ha[i], float(he[i]) / num[i], float(ha[i]) / num[i]});
all ++;
}
sort(f.begin(), f.end());
cout << all << endl;
for(int i = 0; i < all; i ++ )
printf("%04d %d %.3lf %.3lf\n", f[i].id, f[i].num, f[i].avg_e, f[i].avg_area);
return 0;
}