1114 Family Property (25 分)

博客围绕1114家庭房产问题展开,需根据每人家庭信息和房产信息,计算各家庭规模、平均房产套数和面积。采用并查集方法,使用Find()、Union()等函数处理,还提及了数组设置、输入输出处理及遇到的问题,如Find函数符号错误等。

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

思路

  • 又是类似找“朋友圈”——并查集
  • Find(),Union(),cmp(),Init()函数来一套,剩下的就是怎么处理的问题了
  • pre[],isRoot()(求以i为根节点的圈人数)基本是固定的数组
  • vis[i]表示是否存在ID = i的人,本人,父母和孩子的id均要设置为true
  • estate[]和area[]均用double类型存储,开始表示房产数和面积,之后表示平均房产数和面积
  • 以上数组均为maxn(10010)大小
  • 每输入父母或孩子节点的时候,与当前ID进行Union,Union的时候注意编号小的作为根节点,father或mother为-1的时候不进行Union
  • estate和area保存到当前Id下即可,即输入到estate[id]和area[id],
  • cmp函数:先比较平均房产面积,相同再比较根节点编号

Preoblem

  • Find()函数把“=”写成了“==”
  • vis函数没写对父母节点和孩子节点的,其实是借鉴别人代码时没看到写在Union函数中了
  • vector只有泛型的find函数
  • double型输入用%lf输出用%f
#include <bits/stdc++.h>
using namespace std;

const int maxn = 10010;
int pre[maxn];
bool vis[maxn] ={false};
int isRoot[maxn] ={0};
double estate[maxn] = {0};
double area[maxn] = {0};
int n;

int Find(int x)
{
    if(x == pre[x]) return x;
    else return pre[x] = Find(pre[x]);
}

void Union(int x, int y)
{
    vis[y] = 1;//*
    int rx = Find(x);
    int ry = Find(y);
    if(rx != ry){
        if(rx < ry) pre[ry] = rx; //保证根节点编号最小
        else pre[rx] = ry;
    }
}

bool cmp(const int x, const int y)
{
    return area[x] == area[y] ? x < y : area[x] > area[y];
}

void Init()
{
    for(int i = 0; i < maxn; ++i){
        pre[i] = i;
    }
}

int main()
{
    scanf("%d",&n);
    Init();
    for(int i = 0; i < n; ++i){
        int x,father,mother,kidsNum,child;
        scanf("%d%d%d%d",&x,&father,&mother,&kidsNum);
        vis[x] = 1; //编号x存在,避免无效访问
        if(father != -1) Union(x, father);
        if(mother != -1) Union(x, mother);
        for(int j = 0; j < kidsNum; ++j){
            scanf("%d",&child);
            Union(x, child);
        }
        scanf("%lf%lf",&estate[x],&area[x]);
    }
    vector<int> ans;
    for(int i = 0; i < maxn; ++i){
        if(!vis[i]) continue;
        int ri = Find(i);
        vector<int>::iterator it = find(ans.begin(), ans.end(), ri);
        if(it == ans.end()) ans.push_back(ri);
        isRoot[ri]++;
        if(ri != i) {
            estate[ri] += estate[i];
            area[ri] += area[i];
        }
    }
    for(int i = 0; i < ans.size(); ++i){
        int id = ans[i];
        estate[id] /= isRoot[id];
        area[id] /= isRoot[id];
    }
    sort(ans.begin(), ans.end(), cmp);
    printf("%d\n",ans.size());
    for(int i = 0; i < ans.size(); i++){
        int id = ans[i];
        printf("%04d %d %.3f %.3f\n",id,isRoot[id],estate[id],area[id]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值