(PAT)1114. Family Property(并查集)

好久没码算法了,连个并查集都搞得很吃力,贼尴尬。。。

1114. Family Property (25)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 Child1 ... Childk 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; Childi'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

题目大意:有N个家庭,家庭里有你,你爸,你妈,你k个孩子(就是没老婆,可哪来这么多孩子。)然后你有财产和房子面积。现在给你N个家庭,不同家庭可能会有联系,那这样算一家人。最后你输出总共有几家人,每个家庭的人的最小编号(每人有个ID),家族人数,人均财产和房子面积。按人均房子面积降序排序,如果人均房子面积一样,ID小的排前面。

解题思路:这题不算难,但题目罗里吧嗦有点烦。先用并查集(不会的请自行百度)将每个家族的人弄到同一个集合,注意将ID小的做根。同时将出现过的人的ID标记表示出现过该人。再遍历N个家庭,统计出每个家族总的财产和房产面积。再遍历0->9999,将其中出现过的人记录到其对应的家族里去(即该家族人数+1).并根据家族里的变量flag来统计家族个数。最后再循环N次计算出人均值,sort排序输出。(好像是有点烦的 一下子理不清楚。。。)

上代码,注意看注释:

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

int N;
struct Family
{
    int id,fid,mid,est,area;
}fam[1005];
struct Data
{
    int id,num;
    double est,area;
    bool flag; //标记该家族是否存在
}ans[10005];

int Fa[10000];//标记父节点

int find(int a)
{
    
    if(Fa[a]!=a)
        a=find(Fa[a]);
    return a;
}
void merge(int a,int b)
{
    int fa=find(a);
    int fb=find(b);
    if(fa>fb)
        Fa[fa]=fb;//小的做父亲
    else if(fa<fb)
        Fa[fb]=fa;
}

bool vis[10000];//标记人是否出现过
int cou=0; //家族数

bool cmp(Data a,Data b)
{
    if(a.area==b.area)
        return a.id<b.id;
    return a.area>b.area;
}

int main()
{
    int t,k;
    for(int i=0;i<10000;i++)
        Fa[i]=i;
    scanf("%d",&N);
    for(int i=0;i<N;i++)
    {
        scanf("%d%d%d%d",&fam[i].id, &fam[i].fid, &fam[i].mid, &t);
        vis[fam[i].id]=true;
        for(int j=0;j<t;j++)
        {
            scanf("%d",&k);
            vis[k]=true;
            merge(fam[i].id,k);
        }
        scanf("%d%d",&fam[i].est,&fam[i].area);

        if(fam[i].fid!=-1)
        {
            vis[fam[i].fid]=true;
            merge(fam[i].id,fam[i].fid);
        }
        if(fam[i].mid!=-1)
        {
            vis[fam[i].mid]=true;
            merge(fam[i].id,fam[i].mid);
        }
    }

    for(int i=0;i<N;i++)
    {
        int id=find(fam[i].id);
        ans[id].id=id;
        ans[id].est+=fam[i].est*1.0;
        ans[id].area+=fam[i].area*1.0;
        ans[id].flag=true; //表示该家族存在
    }

    for(int i=0;i<N;i++)
    {
        int id=find(fam[i].id);
        
    }

    //统计家族数和家族人数
    for(int i=0;i<10000;i++)
    {
        if(vis[i])
            ans[find(i)].num+=1;
        if(ans[i].flag)//该家族存在
            ++cou;
    }
    for(int i=0;i<N;i++)
    {
        int id=find(fam[i].id);
        
        if(ans[id].flag)
		{
            ans[id].est = (ans[id].est * 1.0 / ans[id].num);
            ans[id].area = (ans[id].area * 1.0 / ans[id].num);
        }
        ans[id].flag=false; //防止重复计算 标记该家族为false
    }
    sort(ans,ans+10000,cmp);
    printf("%d\n", cou);
    for(int i = 0; i < cou; i++)
        //注意04d
        printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].num, ans[i].est, ans[i].area);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值