L2-007 家庭房产 (25 分)

本文档提供了一个C++代码示例,用于解决一个家庭成员及其房产统计的问题。程序通过并查集数据结构来组织家庭关系,并计算每个家庭的人口数、人均房产套数和面积。代码中包含了输入解析、并查集操作以及结果排序和输出。程序首先读取每个人的房产信息,然后通过并查集合并家庭,最后按照人均房产面积降序输出结果。

给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。

输入格式:

输入第一行给出一个正整数N(≤1000),随后N行,每行按下列格式给出一个人的房产:

编号 父 母 k 孩子1 ... 孩子k 房产套数 总面积

其中编号是每个人独有的一个4位数的编号;分别是该编号对应的这个人的父母的编号(如果已经过世,则显示-1);k(0≤k≤5)是该人的子女的个数;孩子i是其子女的编号。

输出格式:

首先在第一行输出家庭个数(所有有亲属关系的人都属于同一个家庭)。随后按下列格式输出每个家庭的信息:

家庭成员的最小编号 家庭人口数 人均房产套数 人均房产面积

其中人均值要求保留小数点后3位。家庭信息首先按人均面积降序输出,若有并列,则按成员编号的升序输出。

输入样例:

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

输出样例:

3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000

我是把所有的信息全都存到了结构体里边,感觉思路和我看的题解上的思路基本是一样的,但是只能对一个点,找不到哪里错误,题解是把信息开到数组里边,感觉实现起来比我的代码要简洁不少。

#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>

#define x first
#define y second

using namespace std;
const int N = 10010;
int n;

vector<pair<int,int>> edge;//有亲属关系,存的是一个二元对
bool st[N];//给用户的编号打标记
int f[N],sizes[N],houses[N],area[N];
//sizes[] 是家庭人数
void init()
{
    for(int i = 0; i < N; i++)
    {
        f[i] = i; sizes[i] = 1; 
    } 
}

inline int find(int x)
{
    return f[x] == x ? x : (f[x] = find(f[x]));
}

void merge(int x, int y)//以编号进行mereg
{
    int fx = find(x); int fy = find(y);
    if(fx != fy)
    {
        if(fx > fy)
            swap(fx, fy);

        f[fy] = fx;
        sizes[fx] += sizes[fy];
        houses[fx] += houses[fy];
        area[fx] += area[fy];
    }
}

bool cmp(int a,int b){//编号
    if(area[a] * sizes[b] == area[b] * sizes[a]){
        return a<b;
    }
    return area[a] * sizes[b] > area[b] * sizes[a];//将除法转成 乘法,细节啊
}

int main()
{
    init();
    cin >> n;
    while(n--)
    {
        int id,f,m,k,child;
        cin >> id >> f >> m;
        st[id] = true;

        if(~f)
            edge.push_back({id,f});
        if(~m)
            edge.push_back({id,m});

        cin >> k;
        while(k--)
        {
            cin >> child;
            edge.push_back({id,child});
        }
        cin >> houses[id] >> area[id];
    }

    for(auto e : edge)
    {
        st[e.x] = st[e.y] = true;
        merge(e.x, e.y); 
    }

    vector<int> ids;
    for(int i = 0; i < N; i++)
    {
        if(st[i] && find(i) == i)
        {
            ids.push_back(i);
        }
    }

    sort(ids.begin(), ids.end(), cmp);
    cout << ids.size() << endl;
    for(auto id: ids)
    {
        printf("%04d %d %.3lf %.3lf\n", id, sizes[id], houses[id]*1.0/sizes[id], area[id]*1.0/sizes[id]);
    }
    return 0;   
} 

我的又臭又长的lj代码,有时间再回来找找bug
/*
A: 10min
B: 20min
C: 30min
D: 40min
*/ 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <vector>
#include <sstream>
#define pb push_back 
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f)) 
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {
    char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
    while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}

typedef pair<int,int>PII;
typedef pair<long,long>PLL;

typedef long long ll;
typedef unsigned long long ull; 
const int N=510,M=4e5+10;
ll n,m,_;
/*
并查集维护,每个集合多维护一个最小的成员的编号
*/
int fa[11010];
int find(int x){
    return x==fa[x]?fa[x]:fa[x]=find(fa[x]);
}
bool st[11010];
struct Node
{
    int minid=10000;
    int fat,mat;
    vector<int>child;
    double Num,num,area;//人口数,房产数,平米总和
}node[11010];

bool cmp(Node a,Node b){
	double A1=a.area/a.Num;
	double A2=b.area/b.Num;
	if(A1==A2){
		return a.minid<b.minid;
	}
	return A1>A2;
}

void merge(int b,int a){//合并到b上去
    int Fa=find(a),Fb=find(b);
    if(Fa!=Fb){
        fa[Fa]=Fb;
        node[Fb].minid=min(node[Fb].minid,node[Fa].minid);
        node[Fb].num+=node[Fa].num;
        node[Fb].area+=node[Fa].area;
        node[Fb].Num+=node[Fa].Num;
    }
}

void solve(){
    for(int i=0;i<N;i++)fa[i]=i,node[i].Num=1,node[i].minid=i;
    //忘了写node[i].minid=i 这句话了
    cin>>n;
    vector<int>t[10100];
    fo(i,1,n){
        int id,fat,mat,k;
        cin>>id>>fat>>mat>>k;
		st[id]=1;
        node[id].minid=id;
        node[id].fat=fat;
        node[id].mat=mat;
        vector<int>tmp;
        while(k--){
            int x;cin>>x;
            tmp.pb(x);
        }
        node[id].child=tmp;
        double num,area;
        cin>>num>>area;
        node[id].num+=num;
        node[id].area+=area;
    }
    // 得先把每个人的信息读进来,才能合并
    fo(i,0,9999){
        if(st[i]){
            int fat=node[i].fat;
            int mat=node[i].mat;
            if(fat!=-1){
                st[fat]=1;
                merge(fat,i);
            }
            if(mat!=-1){
                st[mat]=1;
                merge(mat,i);
            }
            for(auto c:node[i].child){
                st[c]=1;
                merge(i,c);
            }
        }
    }
    int num=0;
    fo(i,0,9999){
        if(st[i]){
            if(fa[i]==i){
                num++;  
            }
        }
    }
    cout<<num<<endl;
	vector<Node>ans;
    fo(i,0,9999){
        if(st[i]){
            if(fa[i]==i){
            	ans.pb(node[i]);

            }
        }
    }
    sort(all(ans),cmp);
    for(auto c:ans){
        printf("%04d %.0lf ",c.minid,c.Num);
		printf("%.3lf %.3lf\n",c.num/c.Num,c.area/c.Num);    	
    }
}

int main(){
	solve();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值