1114 Family Property (25 point(s))

1114 Family Property (25 point(s))

并查集

题目

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:

I D ID ID F a t h e r Father Father M o t h e r Mother Mother k k k C h i l d 1 ⋯ C h i l d k Child_1⋯Child_k Child1​⋯Childk​ M e s t a t e M_{estate} Mestate​ A r e a Area 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; 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 AVGsets AVGarea

where ID is the smallest ID in the family; M is the total number of family members; A V G a r e a AVGarea AVGarea is the average number of sets of their real estate; and A V G a r e a AVGarea 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>
using namespace std;

int fa[10005] = {0}, num[10005] = {0}, ttlnum[10005] = {0};
double area[10005] = {0}, ttlarea[10005] = {0}, estate[10005] = {0}, ttlestate[10005] = {0};
bool flag[10005] = {false}, exist[10005] = {false};

struct Family{
    int id;
    int num = 0;
    double m = 0;
    double area = 0;
};

int find(int i){
    return i == fa[i] ? i : fa[i] = find(fa[i]);
}

void merge(int a, int b){
    int fa1 = find(a);
    int fa2 = find(b);
    (fa1 < fa2) ? (fa[fa2] = fa1) : (fa[fa1] = fa2);
}

bool cmp(Family a, Family b){
    return a.area != b.area ? a.area > b.area : a.id < b.id;
}

int main(){
    int n, k, id, f, m, child;
    scanf("%d", &n);
    vector<Family> ans;
    for(int i = 0; i < 10000; ++i)
        fa[i] = i;
    for(int i = 0; i < n; ++i){
        scanf("%d%d%d", &id, &f, &m);
        exist[id] = exist[f] = exist[m] = true;
        if(f != -1) merge(id, f);
        if(m != -1) merge(id, m);
        scanf("%d", &k);
        for(int j = 0; j < k; ++j){
            scanf("%d", &child);//children
            exist[child] = true;
            merge(id, child);
        }
        scanf("%lf%lf", &estate[id], &area[id]);
    }
    for(int i = 0; i <= 9999 ; ++i){
        if(exist[i]){
            int id = find(i);
            ttlnum[id]++;
            ttlestate[id] += estate[i];
            ttlarea[id] += area[i]; 
        }
    }
    for(int i = 0; i <= 9999; ++i){
        if(ttlnum[i] != 0){
            ans.push_back({i, ttlnum[i], ttlestate[i]/ttlnum[i], ttlarea[i]/ttlnum[i]});
        }
    }
    sort(ans.begin(), ans.end(), cmp);
    printf("%d\n", ans.size());
    for(int i = 0; i < ans.size(); ++i){
        printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].num, ans[i].m, ans[i].area);
    }
    return 0;
}

注意:

  • double类型数据的读入一定要用%lf
  • 每个家庭有重复的人,不要重复计算人头
上一篇:LeetCode 1114. Print in Order--Java解法--并发问题


下一篇:PAT 1114 Family Property