That's all

在线日记

PAT-天梯赛-L2.007家庭房产-未解决(最后一个测试点)

#include<algorithm>
#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using namespace std;
const int Size=10000;// 不用Map实现 数组占用内存太多了···
int Par[Size];
int RenKouShu[Size];
int FangChanZongShu[Size];// 家庭i的房产总数
double FangChanZongMianJi[Size];// 家庭i的房产总面积
double RenJunFangchanTaoShu[Size];
double RenJunFangchanMianJi[Size];

struct Node
{
    int BianHao;
    int JiaTingRenKouShu;
    double RenJunFangChanTaoShu;
    double RenJunFangChanMianJi;
};

bool Cmp(const Node &a, const Node &b)
{
    if(a.RenJunFangChanMianJi!=b.RenJunFangChanMianJi)
        return a.RenJunFangChanMianJi>b.RenJunFangChanMianJi;
    return a.BianHao<b.BianHao;
}

int Get_Par(int a)
{
    if(a!=Par[a])
         Par[a]=Get_Par(Par[a]);
    return Par[a];
}

void Merge(int a, int b)
{
        //需要判断各自的祖先节点 编号小的那个为最终祖先 因为最后输出要求
        int ParA=Get_Par(a);
        int ParB=Get_Par(b);
        if(ParA<ParB)
        {
            //cout<<"This Merge the"<<ParB<<"Big than"<<ParA<<" "<<endl;
            Par[ParB]=ParA;
            //cout<<"And the Par of "<<ParB<<"is "<<ParA<<endl;
            RenKouShu[ParA]+=RenKouShu[ParB];
            FangChanZongShu[ParA]+=FangChanZongShu[ParB];
            FangChanZongMianJi[ParA]+=FangChanZongMianJi[ParB];
        }
        else if(ParA>ParB)
        {
            //cout<<"This Merge the"<<ParA<<"Big than"<<ParB<<" "<<endl;

            Par[ParA]=ParB;
            //cout<<"And the Par of "<<ParA<<"is "<<ParB<<endl;
            RenKouShu[ParB]+=RenKouShu[ParA];
            FangChanZongShu[ParB]+=FangChanZongShu[ParA];
            FangChanZongMianJi[ParB]+=FangChanZongMianJi[ParA];
        }
}

int  main()
{
    //ifstream cin("SARS.txt");
    int N;
    cin>>N;
    int BianHao,Fu,Mu,k,Child,FangChanShu;
    double ZongMianji;
    memset(Par,0,sizeof(Par));
    memset(FangChanZongShu,0,sizeof(FangChanZongShu));
    memset(FangChanZongMianJi,0,sizeof(FangChanZongMianJi));

    for(int i=0; i<N; i++)
    {
        cin>>BianHao>>Fu>>Mu;
        if(Par[BianHao]==0){
            RenKouShu[BianHao]=1;
            Par[BianHao]=BianHao;
        }
        if(Fu!=-1)
        {
            if(Par[Fu]==0){
                RenKouShu[Fu]=1;
                Par[Fu]=Fu;
            }
            Merge(BianHao,Fu);
        }
        if(Mu!=-1)
        {
            if(Par[Mu]==0){
                RenKouShu[Mu]=1;
                Par[Mu]=Mu;
            }
            Merge(BianHao,Mu);
        }
        cin>>k;
        while(k--)
        {
            cin>>Child;
            if(Par[Child]==0){
                RenKouShu[Child]=1;
                Par[Child]=Child;
            }
            Merge(BianHao,Child);
        }
        cin>>FangChanShu;
        FangChanZongShu[Get_Par(BianHao)]+=FangChanShu;
        cin>>ZongMianji;
        FangChanZongMianJi[Get_Par(BianHao)]+=ZongMianji;
    }
    int ans=0;
    vector<Node>Vec;
    Node node;
    for(int i=0;i<10000; i++)
    {
        if(Par[i]==i&&RenKouShu[i]!=0){
            ans++;
            node.BianHao=i;
            node.JiaTingRenKouShu=RenKouShu[i];
            node.RenJunFangChanTaoShu = FangChanZongShu[i]*1.0/RenKouShu[i];
            node.RenJunFangChanMianJi = FangChanZongMianJi[i]/RenKouShu[i];
            Vec.push_back(node);
        }
    }
    cout<<ans<<endl;
    sort(Vec.begin(),Vec.end(),Cmp);
    for(int i=0; i<Vec.size();i++)
    {
        printf("%04d %d %.3f %.3f\n",Vec[i].BianHao,Vec[i].JiaTingRenKouShu,Vec[i].RenJunFangChanTaoShu,Vec[i].RenJunFangChanMianJi);
    }
    return 0;
}

基础并查集 ···

过几天重新敲一下···

还差最后一个测试点···


阅读更多
个人分类: 天梯赛—练习集
上一篇PAT天梯赛-L2-001 紧急救援
下一篇经典重拾-从前言开始
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭