PTA甲级 1114 Family Property (C++)

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 k k C h i l d 1 ⋯ C h i l d k Child_1⋯Child_k Child1Childk M e s t a t e M_{estate} 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 ) k (0≤k≤5) k(0k5) is the number of children of this person; C h i l d i Child_i Childi's are the ID's of his/her children; M e s t a t e M_{estate} 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 A V G s e t s AVG_{sets} AVGsets A V G a r e a AVG_{area} AVGarea
where ID is the smallest ID in the family; M is the total number of family members; A V G s e t s AVG_{sets} AVGsets is the average number of sets of their real estate; and A V G a r e a AVG_{area} 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

Caution:

柳神yyds

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-09-06 16:56:17
// All rights reserved.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Data{
    int id, fid, mid, numOfc, sets, area;
    int cid[6];
};

struct Family{
    int rootID, people, sets, area;
    double averageSets, averageArea;
    bool valid = false;
};

Data datas[10010];
Family family[10010];
int father[10000];
bool vis[10000];

int ID2sets[10000];
int ID2area[10000];

int findFather(int x){
    while(father[x] != x) x = father[x];
    return x;
}

void Union(int a, int b){
    int fa = findFather(a);
    int fb = findFather(b);
    if(fa < fb) father[fb] = fa;
    else if(fa > fb) father[fa] = fb;
}

bool cmp(Family &a, Family &b){
    if(a.averageArea != b.averageArea) return a.averageArea > b.averageArea;
    return a.rootID < b.rootID;
}

int main(){
    int n;
    scanf("%d", &n);

    for(int i = 0; i < 10000; ++i) father[i] = i;

    for(int i = 0; i < n; ++i){
        scanf("%d %d %d %d", &datas[i].id, &datas[i].fid, &datas[i].mid, &datas[i].numOfc);
        vis[datas[i].id] = true;
        
        if(datas[i].fid != -1){
            vis[datas[i].fid] = true;
            Union(datas[i].id, datas[i].fid);
        }
        if(datas[i].mid != -1){
            vis[datas[i].mid] = true;
            Union(datas[i].id, datas[i].mid);
        }

        for(int j = 0; j < datas[i].numOfc; ++j){
            scanf("%d", &datas[i].cid[j]);

            vis[datas[i].cid[j]] = true;
            Union(datas[i].id, datas[i].cid[j]);
        }

        scanf("%d %d", &datas[i].sets, &datas[i].area);
    }

    for(int i = 0; i < n; ++i){
        int id = findFather(datas[i].id);

        family[id].rootID = id;
        family[id].area += datas[i].area;
        family[id].sets += datas[i].sets;
        family[id].valid = true;
    }

    int cnt = 0;
    for(int i = 0; i < 10010; ++i){
        if(vis[i]) family[findFather(i)].people++;
        if(family[i].valid) cnt++;
    }

    for(int i = 0; i < 10010; ++i){
        if(family[i].valid){
            family[i].averageArea = family[i].area * 1.0 / family[i].people;
            family[i].averageSets = family[i].sets * 1.0 / family[i].people;
        }
    }
    
    sort(family, family + 10010, cmp);

    printf("%d\n", cnt);
    for(int i = 0; i < cnt; ++i) 
        printf("%04d %d %.3lf %.3lf\n", family[i].rootID, family[i].people, family[i].averageSets, family[i].averageArea);

    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

负反馈循环

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值