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
#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
int fa[10001];
bool visit[10001] = {false};
struct node{
int id;
int num_people;;
int num_estate;
int num_area;
double ave_area;
double ave_num;
bool flag;
};
int find_father(int x){
int a = x;
while (x != fa[x])
{
x = fa[x];
}
while (a != fa[a])
{
int z = a;
a = fa[a];
fa[z] = x;
}
return x;
}
bool cmp(node a, node b){
return a.ave_area != b.ave_area ? a.ave_area > b.ave_area : a.id < b.id;
}
void union_point(int a, int b){
int fa_a = find_father(a);
int fa_b = find_father(b);
if(fa_a > fa_b)
fa[fa_a] = fa_b;
if(fa_b > fa_a)
fa[fa_b] = fa_a;
}
int main(){
int n;
cin >> n;
unordered_map<int, int> num_map;
unordered_map<int, int> area_map;
for(int i = 0; i < 10001; i++){
fa[i] = i;
}
for(int i = 0; i < n; i++){
int temp_a, temp_b, temp_c, k;
scanf("%d %d %d %d", &temp_a, &temp_b, &temp_c, &k);
visit[temp_a] = true;
if(temp_b != -1){
union_point(temp_a, temp_b);
visit[temp_b] = true;
}
if(temp_c != -1){
union_point(temp_a, temp_c);
visit[temp_c] = true;
}
for(int j = 0; j < k; j++){
int child;
scanf("%d", &child);
union_point(temp_a, child);
visit[child] = true;
}
int num_estate, area_estate;
scanf("%d %d", &num_estate, &area_estate);
num_map[temp_a] = num_estate;
area_map[temp_a] = area_estate;
// int fa_a = find_father(temp_a);
// bool flag = true;
// for(auto it : num_map){
// int fa_b = find_father(it.first);
// int temp_num, temp_area;
// if(fa_a == fa_b){
// temp_num = num_map[it.first];
// temp_area = area_map[it.first];
// num_map[it.first] = 0;
// area_map[it.first] = 0;
// num_map[fa_a] += temp_num;
// area_map[fa_a] += temp_area;
// }
// }
}
vector<node> vec(10001);
for(auto it : num_map){
int fa_a = find_father(it.first);
vec[fa_a].id = fa_a;
vec[fa_a].num_estate += num_map[it.first];
vec[fa_a].num_area += area_map[it.first];
vec[fa_a].flag = true;
}
// for(auto it : num_map){
// if(it.second != 0){
// node temp;
// temp.id = it.first;
// temp.num_area = area_map[it.first];
// temp.num_estate = it.second;
// // cout << temp.id << " " << area_map[it.first] << " " it.second << endl;
// vec.push_back(temp);
// // cout << temp.id << " " << temp.num_area << " " << temp.num_estate << endl;
// }
// }
int cnt = 0;
for(int i = 0; i < 10001; i++){
if(visit[i]){
int root = find_father(i);
vec[root].num_people++;
}
if(vec[i].flag){
cnt++;
}
}
for(int i = 0; i < 10001; i++){
if(vec[i].flag){
vec[i].ave_area = (double)(vec[i].num_area * 1.0 / vec[i].num_people);
vec[i].ave_num = (double)(vec[i].num_estate * 1.0 / vec[i].num_people);
}
}
sort(vec.begin(), vec.end(), cmp);
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++){
printf("%04d %d %.3f %.3f\n", vec[i].id, vec[i].num_people, vec[i].ave_num, vec[i].ave_area);
}
return 0;
}
本文介绍了一种用于收集和分析家庭成员及其房产数据的算法。该算法通过输入家庭成员信息和房产详情,能够计算每个家庭的规模,房产平均面积及套数。通过分析,算法能够输出家庭数量,并按家庭平均房产面积降序排列。
648

被折叠的 条评论
为什么被折叠?



