1114 Family Property (并查集)

本文介绍了一种用于统计家庭成员及家庭财产的算法,通过并查集数据结构整合家庭成员信息,计算各独立家庭的成员数量、平均房产数量及面积。最终输出家庭总数及各家庭的详细统计数据。

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

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

/**
本题题意 :告诉 总共家庭的 包括 自己的id , 父母的pid 孩子的个数k, 孩子的 k个id 有几栋房子, 房子的总面积,
            这些家庭间 有的人id 相同就说明 它们属于一个家庭
           最终计算出 所有独立的家庭数, 并输出这些独立家庭中 最小家庭成员id,家庭成员人数,每个平均拥有的房产, 以及房子的平均面积 (保留3位小数)
           输出时的顺序是 房子的平均面积 按从大到小, 如果房子平均面积相等 就按照 id 从小到大排列
本题思路:
           运用并查集, set[i] 为的下标代表每个人的id (id为4位10进制数构成,因此范围为 10的4次方)
           因为最后要排序 设定一个结构体保存 最小的id , 家庭成员人数,房产证数量, 房子面积
           1.通过set[i]  能够计算出,每个家庭的人数  (visit[i] == true ) num[find(i)].number[i]++
           2.单独开个est数组,are数组 是因为防止 num数组中的estate,area重复多加 est数组能够计算出一个家庭总房产数量
            are数组能够计算出一个家庭的总面积 (当 est[i](表示 成员 i所有的房产数量) > 0 , num[find(i)] += est[i] )    
            (当 art[i](表示 成员 i所有的房产面积) > 0 , num[find(i)] += are[i])
            最后 通过 总面积 (总数量) / 总人数(num[i].number) 就为平均数
            注意 c++ int 转 double时 必须强制转型;
            num[i].avgEstate = double(num[i].estate * 1.0 / num[i].number);
            num[i].avgArea = double(num[i].area * 1.0 / num[i].number);

            
**/

   

#include<iostream>
#include<algorithm>
using namespace std;
const int maxsize = 1e4 + 5;
struct Node{
	int id,number, estate, area;       //代表最小的id , 家庭成员人数,房产证数量, 房子面积 
	double avgEstate, avgArea;         //平均房子数量, 平均房子面积 
} num[maxsize];
//单独开个est数组,are数组 是因为防止 num数组中的estate,area重复多加 
int n, set[maxsize], est[maxsize], are[maxsize]; 
bool visit[maxsize];
int find(int i){
	return set[i] == i? i : set[i] = find(set[i]);
}
void merge(int a, int b){
	int FaA = find(a);
	int FaB = find(b);
	 if(FaA != FaB)
	 	set[FaA] = FaB;
}
bool cmp(Node a, Node b){
	if(a.avgArea != b.avgArea)
		return a.avgArea > b.avgArea;
	else
		return a.id < b.id;
}
void init(){
	for(int  i = 0; i < maxsize; i++)
		set[i] = i;
}
int main(){
	int id, pid, k, childid, estate, area;
	scanf("%d", &n);
	init();
	//开始合并所有人的id 
	for(int i = 0; i < n; i++){
		scanf("%d", &id);
		visit[id] = true;
		for(int j = 0; j < 2; j++){
			scanf("%d", &pid);
			visit[pid] = true;
			if(pid != -1){
				if(find(id) < find(pid))  //  要将最小的点当作最终的家庭集合点, 那么比较大小的也是最终点 
					merge(pid, id);		
				else
					merge(id, pid);
			}
		}
		scanf("%d", &k);
		for(int c = 0; c < k; c++){
			scanf("%d", &childid);
			visit[childid] = true;
			find(childid) < find(id) ? merge(id, childid) : merge(childid, id); //这个判断最好写在merge函数内部,合并时,大的id 连接小的id 
		}
		scanf("%d%d", &estate, &area);
		est[find(id)] += estate;      //要将 家庭已经有的 面积 加上 
		are[find(id)] += area;
	}
	for(int i = 0; i < maxsize; i++){ //开始计算集合(家庭)中(人)结点的数量 
		if(visit[i] == true){
			num[find(i)].id = find(i);	
			num[find(i)].number++;
		}
	}
	int sum = 0;//计算家庭的数量 ,计算每个最终结点(集合(家庭))的estate,area的数量和 
	for(int i = 0; i < maxsize; i++){
		if(num[i].number != 0) 
			sum++;
		if(est[i] != 0 && are[i] != 0){
			num[find(i)].estate += est[i];
			num[find(i)].area += are[i];
		}
		 
	}
	
	for(int i = 0; i < maxsize; i++){
		if(num[i].estate > 0 && num[i].area > 0) {
			num[i].avgEstate = double(num[i].estate * 1.0 / num[i].number);
			num[i].avgArea = double(num[i].area * 1.0 / num[i].number);
		} 
	}
	sort(num, num + maxsize, cmp);
	printf("%d\n", sum);
	for(int i = 0; i < sum; i++){
		printf("%04d %d %.3lf %.3lf\n", num[i].id, num[i].number, num[i].avgEstate, num[i].avgArea);
	}
	
	return 0;
}

 

资源下载链接为: https://pan.quark.cn/s/9648a1f24758 这个HTML文件是一个专门设计的网页,适合在告白或纪念日这样的特殊时刻送给女朋友,给她带来惊喜。它通过HTML技术,将普通文字转化为富有情感和创意的表达方式,让数字媒体也能传递深情。HTML(HyperText Markup Language)是构建网页的基础语言,通过标签描述网页结构和内容,让浏览器正确展示页面。在这个特效网页中,开发者可能使用了HTML5的新特性,比如音频、视频、Canvas画布或WebGL图形,来提升视觉效果和交互体验。 原本这个文件可能是基于ASP.NET技术构建的,其扩展名是“.aspx”。ASP.NET是微软开发的一个服务器端Web应用程序框架,支持多种编程语言(如C#或VB.NET)来编写动态网页。但为了在本地直接运行,不依赖服务器,开发者将其转换为纯静态的HTML格式,只需浏览器即可打开查看。 在使用这个HTML特效页时,建议使用Internet Explorer(IE)浏览器,因为一些老的或特定的网页特效可能只在IE上表现正常,尤其是那些依赖ActiveX控件或IE特有功能的页面。不过,由于IE逐渐被淘汰,现代网页可能不再对其进行优化,因此在其他现代浏览器上运行可能会出现问题。 压缩包内的文件“yangyisen0713-7561403-biaobai(html版本)_1598430618”是经过压缩的HTML文件,可能包含图片、CSS样式表和JavaScript脚本等资源。用户需要先解压,然后在浏览器中打开HTML文件,就能看到预设的告白或纪念日特效。 这个项目展示了HTML作为动态和互动内容载体的强大能力,也提醒我们,尽管技术在进步,但有时复古的方式(如使用IE浏览器)仍能唤起怀旧之情。在准备类似的个性化礼物时,掌握基本的HTML和网页制作技巧非常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值