1114 Family Property

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

Solution

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 10010;

int g_housholders[maxn] = {0};
int g_estates[maxn] = {0};
int g_areas[maxn] = {0};
int n;

struct Family
{
    int m_family_number = 0;
    int m_total_estates = 0;
    int m_total_areas = 0;
    int m_housholdersID = -1;

    double m_average_estates = 0.0;
    double m_average_areas = 0.0;
}family[maxn];

void init(int id)
{
    if(g_housholders[id] == -1) {
        g_housholders[id] = id;
    }
}

int findHousemaster(int x)
{
    int housemaster = x;
    while (housemaster != g_housholders[housemaster])
    {
        housemaster = g_housholders[housemaster];
    }

    while (x != g_housholders[x])
    {
        int f = x;
        x = g_housholders[x];
        g_housholders[f] = housemaster;
    }
    
    return housemaster;
}

void unionHouseholder(int a, int b)
{
    int faA = findHousemaster(a);
    int faB = findHousemaster(b);
    if(faA != faB) {
        int min = faA > faB ? faB : faA;
        int max = faA + faB - min;
        g_housholders[max] = min;
    }
}

bool cmp(Family a, Family b)
{
    if(a.m_average_areas != b.m_average_areas) {
        return a.m_average_areas > b.m_average_areas;
    }
    else {
        return a.m_housholdersID < b.m_housholdersID;
    }
}

int main()
{
    memset(g_housholders, -1, sizeof(g_housholders));
    scanf("%d", &n);

    int id, fatherId, motherId;
    int k, childId;
    for(int i = 0; i < n; ++i) {
        scanf("%d %d %d %d", &id, &fatherId, &motherId, &k);
        if(g_housholders[id] == -1) {
            g_housholders[id] = id;
        }

        if(fatherId != -1) {
            init(fatherId);
            unionHouseholder(id, fatherId);
        }
        if(motherId != -1) {
            init(motherId);
            unionHouseholder(id, motherId);
        }

        for(int j = 0; j < k; ++j) {
            scanf("%d", &childId);
            init(childId);
            unionHouseholder(id, childId);
        }

        scanf("%d %d", &g_estates[id], &g_areas[id]);
    }

    for(int i = 0; i < maxn; ++i) {
        if(g_housholders[i] != -1) {
            int housholdersId = findHousemaster(i);
            family[housholdersId].m_housholdersID = housholdersId;
            family[housholdersId].m_family_number++;
            family[housholdersId].m_total_estates += g_estates[i];
            family[housholdersId].m_total_areas += g_areas[i];
        }
    }

    int total_family = 0;
    for(int i = 0; i < maxn; ++i){
        if(family[i].m_housholdersID != -1) {
            total_family++;
            family[i].m_average_estates = family[i].m_total_estates*1.0 / family[i].m_family_number;
            family[i].m_average_areas = family[i].m_total_areas*1.0 / family[i].m_family_number;
        }
    }

    sort(family, family+maxn, cmp);
    printf("%d\n", total_family);

    for(int i = 0; i < total_family; ++i) {
        printf("%04d %d %.3f %.3f\n", family[i].m_housholdersID
                                , family[i].m_family_number
                                , family[i].m_average_estates
                                , family[i].m_average_areas);
    }

    return 0;
}
posted @ 2024-02-06 15:35  王清河  阅读(19)  评论(0编辑  收藏  举报