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<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;
}

 

`Assignment to readonly property` 错误通常表示尝试对只读属性进行赋值操作。以下是一些可能的解决方案: #### 检查属性声明 在Objective - C中,若属性被声明为只读(`readonly`),则不能直接对其赋值。可将属性声明修改为可读写(`readwrite`)。 ```objc // 只读声明 @property (readonly) NSString *name; // 修改为可读写 @property (readwrite, nonatomic, strong) NSString *name; ``` #### 使用KVC(键值编码) 在Objective - C里,即便属性被声明为只读,也能使用KVC来绕过只读限制进行赋值。 ```objc #import <Foundation/Foundation.h> @interface Person : NSObject @property (readonly, nonatomic, strong) NSString *name; @end @implementation Person - (instancetype)init { self = [super init]; if (self) { _name = @"default"; } return self; } @end int main(int argc, const char * argv[]) { @autoreleasepool { Person *p = [[Person alloc] init]; [p setValue:@"newName" forKey:@"name"]; NSLog(@"%@", p.name); } return 0; } ``` #### 检查JavaScript中的属性赋值 在JavaScript里,有些属性是只读的,不能直接赋值。如在iOS的Safari 10以下版本中,直接给 `style` 属性赋值会报错。可通过单独设置样式属性来避免该问题。 ```javascript let childNode = document.createElement(&#39;span&#39;); // 不要直接赋值 style // childNode.style = "width: 20px;height: 20px;border-radius: 10px;background: #FB7299;color: #FFFFFF;display: inline-block;position: absolute;font-size: 14px;font-family: PingFangSC-Regular;line-height:20px;"; // 单独设置样式属性 childNode.style.width = &#39;20px&#39;; childNode.style.height = &#39;20px&#39;; childNode.style.borderRadius = &#39;10px&#39;; childNode.style.background = &#39;#FB7299&#39;; childNode.style.color = &#39;#FFFFFF&#39;; childNode.style.display = &#39;inline-block&#39;; childNode.style.position = &#39;absolute&#39;; childNode.style.fontSize = &#39;14px&#39;; childNode.style.fontFamily = &#39;PingFangSC-Regular&#39;; childNode.style.lineHeight = &#39;20px&#39;; childNode.style.top = e.offsetY - 10 + &#39;px&#39;; childNode.style.left = e.offsetX - 10 + &#39;px&#39;; ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值