PAT 1114 Family Property

博客围绕家庭房产数据收集展开,给出输入输出规范及示例。需根据每人家庭成员和房产信息,计算家庭规模、房产平均套数和面积。考察并查集,未用数组实现而是利用map,还涉及对每个cluster计数,以及深度优先搜索版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

考察并查集,这道题没有使用数组实现,利用map.

还要对每个cluster进行计数

这里要注意合并时,当发现两个根相同时,

什么也不做

#include <map>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

struct family{
    int root;
    int num;
    double avg1;
    double avg2;
};
bool cmp(family& f1,family& f2){
    if(abs(f1.avg2-f2.avg2)<=1.0e-6){
        return f1.root<f2.root;
    }
    return f1.avg2>f2.avg2;
}
int N;
int find_root(int x,map<int,int>& findSet){
    while (x!=findSet[x]){
        x=findSet[x];
    }
    return x;
}

void setUnion(int x,int y,map<int,int>&findSet,map<int,int>&countSet){
    int find_x=find_root(x,findSet);
    int find_y=find_root(y,findSet);
    if (find_x<find_y){
        findSet[find_y]=find_x;
        countSet[find_x]+=countSet[find_y];
        countSet[find_y]=0;
    } else if(find_x>find_y){
        findSet[find_x]=find_y;
        countSet[find_y]+=countSet[find_x];
        countSet[find_x]=0;
    }
}

int main() {
    cin>>N;
    map<int,int> findSet;
    map<int,int> countSet;
    map<int,double> numSet;
    map<int, double> areaSet;
    for (int i = 0; i < N; ++i) {
        int id,father,mother,k;
        cin>>id>> father>>mother>>k;
        if(findSet.find(id)==findSet.end()){
            findSet[id]=id;
            countSet[id]=1;
        }
        if(father!=-1&&findSet.find(father)==findSet.end()){
            findSet[father]=father;
            countSet[father]=1;
        }
        if(mother!=-1&&findSet.find(mother)==findSet.end()){
            findSet[mother]=mother;
            countSet[mother]=1;
        }
        if(father!=-1) {
            setUnion(id, father, findSet, countSet);
        }
        if(mother!=-1) {
            setUnion(id, mother, findSet, countSet);
        }
        for (int j = 0; j < k; ++j) {
            int val;
            cin>>val;
            if(findSet.find(val)==findSet.end()){
                findSet[val]=val;
                countSet[val]=1;
            }
            setUnion(id,val,findSet,countSet);
        }
        double num,area;
        cin>>num>>area;
        numSet[id]=num;
        areaSet[id]=area;
    }
    map<int, double> total1;
    map<int ,double> total2;
    for(auto pair:numSet){
        int id=pair.first;
        double num1=pair.second;
        double num2=areaSet[id];
        int root=find_root(id,findSet);
        total1[root]+=num1;
        total2[root]+=num2;
    }
    vector<family> vector1;
    for(auto pair:total1){
        family fa{};
        fa.root=pair.first;
        int id=fa.root;
        fa.num=countSet[id];
        int num=fa.num;
        fa.avg1=total1[id]/num;
        fa.avg2=total2[id]/num;
        vector1.push_back(fa);
    }
    sort(vector1.begin(),vector1.end(),cmp);
    cout<<vector1.size()<<endl;
    for (auto &fa:vector1){
        printf("%04d %d %.3f %.3f\n",fa.root,fa.num,fa.avg1,fa.avg2);
    }
    return 0;
}

深度优先搜索版本:

#include <iostream>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<climits>
using namespace std;
struct Famliy{
    int sid;
    int num;
    double avgset;
    double avgarea;
};
map<int,vector<int>> graph;
map<int,int> map1;//保存setnum
map<int,int> map2;//保存area
map<int,bool> visited;
int minNum;
int cnt;
int totalArea;
int totalSet;
void dfs(int x){
    if(x<minNum){
        minNum=x;
    }
    visited[x]=true;
    cnt++;
    totalArea+=map2[x];
    totalSet+=map1[x];
    for(auto v:graph[x]){
        if(!visited[v]){
            dfs(v);
        }
    }
}
bool cmp(Famliy* f1,Famliy* f2){
    if(f1->avgarea==f2->avgarea){
        return f1->sid<f2->sid;
    }
    return f1->avgarea>f2->avgarea;
}
int main()
{
    int N;
    cin>>N;
    set<int> set1;
    for(int i=0;i<N;i++){
        int id,f,m,k;
        cin>>id>>f>>m>>k;
        set1.insert(id);
        if(f!=-1){
            set1.insert(f);
               graph[id].push_back(f);
        graph[f].push_back(id);
        }
        if(m!=-1){
            set1.insert(m);
            graph[id].push_back(m);
        graph[m].push_back(id);
        }
        for(int j=0;j<k;j++){
            int val;
            cin>>val;
            set1.insert(val);
            graph[id].push_back(val);
            graph[val].push_back(id);
        }
        int nset,narea;
        cin>>nset>>narea;
        map1[id]=nset;
        map2[id]=narea;
    }
    vector<Famliy*> vector1;
    for(auto x:set1){
      //  cout<<x<<endl;
        minNum=INT_MAX;
        cnt=0;
        totalArea=0;
        totalSet=0;
        if(!visited[x]){
         //       cout<<x<<endl;
            dfs(x);
            Famliy* f=new Famliy();
            f->sid=minNum;
            f->num=cnt;
            f->avgset=1.0*totalSet/cnt;
            f->avgarea=1.0*totalArea/cnt;
            vector1.push_back(f);
        }
    }
    cout<<vector1.size()<<endl;
    sort(vector1.begin(),vector1.end(),cmp);
    for(auto f:vector1){
        printf("%04d %d %.3f %.3f\n",f->sid,f->num,f->avgset,f->avgarea);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值