PAT (Advanced Level) Practise 1114 Family Property (25) 并查集orDFS

别再自己写消息提示了!5分钟用WPF+Material Design打造专业级Snackbar(支持.NET 6) 本文为WPF开发者提供了一份快速集成专业级Snackbar消息提示的实战指南。通过引入MaterialDesignThemes开源库,在.NET 6 WPF项目中,仅需5分钟即可告别传统的MessageBox,实现遵循Material Design规范、动画流畅、支持队列管理的非侵入式Snackbar控件,显著提升应用的用户体验与现代化交互水平。 阅读详情

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

并查集,将所有有亲戚关系的节点全部合并到一个并查集里,子节点将地产的信息和人数信息也要合并到父节点里面去,但是感觉太长了。。。看网上有直接用DFS的

就七八十行,输了。。。



#include <cstdio>
#include <cstring>
#include <map>
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

struct node {
	int id, cou; //cou为集合中的元素数目
	double suma, sums;
	node() { cou = 0; suma = 0; sums = 0; }
	node(int id) : id(id) { cou = 0; suma = 0; sums = 0; }
}fam[10], f[11111], ans[11111];

const int INF = 0x7fffffff;
int N;

bool cmp(node a, node b) {
	if (fabs(a.suma - b.suma) < 1e-8) {
		return a.id < b.id;
	}
	else {
		return a.suma > b.suma;
	}
}

int getf(int x) {
	return f[x].id == x ? x : f[x].id = getf(f[x].id);
}

void Merge(int x, int y) {
	int xx = getf(x), yy = getf(y);
	int fx = min(xx, yy), fy = max(xx, yy);
	if (fx != fy) {
		f[fy].id = f[fx].id; //父节点吸收吸收子节点
		f[fx].sums += f[fy].sums;
		f[fx].suma += f[fy].suma;
		f[fx].cou += f[fy].cou;
	}
}

int main()
{
	scanf("%d", &N);
	map<int, int> m; //用来记录ID的出现情况,m[i]不为零说明出现过
	bool vis[10001]; //用来记录一个ID的重复情况,为true时说明以前出现过

	//并查集初始化
	for (int i = 0; i <= 9999; i++) {
		f[i].id = i;
	}

	memset(vis, false, sizeof(vis));
	for (int t = 1; t <= N; t++) {
		int id, fa, ma, k, len, Set, Area, minn, index, ff;
		scanf("%d%d%d", &id, &fa, &ma);
		k = 0; //fam数组的长度
		minn = INF; //minn用来存根节点的最小值
		index = 0; //index为输入的一组ID里面根节点最小的所在下标
		m[id]++;  //说明这个ID出现过
		ff = getf(id);
		if (ff < minn) {
			minn = ff;
			index = 0;
		}
		fam[k++] = node(id);
		if (fa != -1) {
			m[fa]++;
			fam[k] = node(fa);
			ff = getf(fa);
			if (ff < minn) {
				minn = ff;
				index = k;
			}
			k++;
		}
		if (ma != -1) {
			m[ma]++;
			fam[k] = node(ma);
			ff = getf(ma);
			if (ff < minn) {
				minn = ff;
				index = k;
			}
			k++;
		}

		scanf("%d", &len);
		while (len--) {
			scanf("%d", &fam[k].id);
			m[fam[k].id]++;
			ff = getf(fam[k].id);
			if (ff < minn) {
				minn = ff;
				index = k;
			}
			k++;
		}
		scanf("%d%d", &Set, &Area);
		int c = 0;
		for (int i = 0; i < k; i++) {
			Merge(fam[index].id, fam[i].id);

			//在集合里去重,也就是以前加入集合的不用再加入了
			if (!vis[fam[i].id]) {
				vis[fam[i].id] = true;
				c++;
			}
		}
		int root = getf(fam[index].id);
		f[root].sums += Set;
		f[root].suma += Area;
		f[root].cou += c;
	}
	for (int i = 0; i <= 9999; i++) {
		f[i].id = getf(f[i].id);
	}
	int LEN = 0;
	for (int i = 0; i <= 9999; i++) {
		//说明m[i]在输入的数里面
		if (m[i] != 0) {
			if (f[i].id == i) {
				ans[LEN] = f[i];
				ans[LEN].suma /= ans[LEN].cou;
				ans[LEN].sums /= ans[LEN].cou;
				LEN++;
			}
		}
	}
	sort(ans, ans + LEN, cmp);
	printf("%d\n", LEN);
	for (int i = 0; i < LEN; i++) {
		printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].cou, ans[i].sums, ans[i].suma);
	}
	return 0;
}


用DFS直接找连通域并累加人数和地产信息


#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 10000 + 10;
int N, C, Set[maxn], Area[maxn], ID;
double S, A;
bool f[maxn];

struct node {
	int id, cou;
	double a_s, a_a;
	node(int id = 0, int cou = 0, double a_s = 0, double a_a = 0) : id(id), cou(cou), a_s(a_s), a_a(a_a) {};
}p[maxn];

bool cmp(node a, node b) {
	if (fabs(a.a_a - b.a_a) < 1e-8) {
		return a.id < b.id;
	}
	else {
		return a.a_a > b.a_a;
	}
}

vector<int> m[maxn];

void dfs(int x) {
	ID = min(ID, x);
	C++; //累加人数
	f[x] = false; //标记不再重走
	S += (1.0 * Set[x]); //累加地产信息,地产挂在第一个id头上,其他的都为0
	A += (1.0 * Area[x]);
	for (int i = 0; i < m[x].size(); i++) {
		if (f[m[x][i]]) {
			dfs(m[x][i]);
		}
	}
}

int main()
{
	scanf("%d", &N);
	memset(f, false, sizeof(f));
	for (int t = 0; t < N; t++) {
		int id, fa, ma, len, tem;
		scanf("%d%d%d", &id, &fa, &ma);
		f[id] = true;  //记录一个数出现过
		if (fa != -1) {
			f[fa] = true;
			m[id].push_back(fa);
			m[fa].push_back(id);
		}
		if (ma != -1) {
			f[ma] = true;
			m[id].push_back(ma);
			m[ma].push_back(id);
		}
		scanf("%d", &len);
		while (len--) {
			scanf("%d", &tem);
			f[tem] = true;
			m[id].push_back(tem);
			m[tem].push_back(id);
		}
		scanf("%d%d", &Set[id], &Area[id]);
	}
	int LEN = 0;
	//0 - 9999扫描
	for (int i = 0; i < 10000; i++) {
		//输入过的
		if (f[i]) {
			ID = 10000;
			C = 0;
			S = A = 0;
			dfs(i);
			p[LEN++] = node(ID, C, S / (1.0 * C), A / (1.0 * C));
		}
	}
	sort(p, p + LEN, cmp);
	printf("%d\n", LEN);
	for (int i = 0; i < LEN; i++) {
		printf("%04d %d %.3f %.3f\n", p[i].id, p[i].cou, p[i].a_s, p[i].a_a);
	}
	return 0;
}




39、信号流图:解决线性代数方程组的图论方法 本文介绍了信号流图理论在求解线性代数方程组中的应用,重点阐述了Coates方法和Mason方法的原理、步骤及对比分析。通过图论视角,将矩阵运算转化为对有向图结构的分析,利用1-因子、1-阶连接、前向路径和反馈回路等概念,推导出方程组解的拓扑公式。文章结合具体实例演示两种方法的计算过程,并总结其在电路分析、控制系统和通信系统等领域的拓展应用,展示了信号流图理论的直观性与有效性。 阅读详情

相关推荐

行列式详解:从定义到应用

本文系统介绍了行列式的核心概念和应用。首先从二阶、三阶行列式入手,逐步扩展到n阶行列式的定义,讲解了余子式和代数余子式的概念。其次,详细阐述了行列式的重要性质,包括转置不变性、行列交换变号等基本性质以及乘积性质等推论。在计算方法方面,介绍了按行展开法、化上三角矩阵法等实用技巧,并列举了特殊行列式的计算案例。最后重点讨论了行列式在克拉默法则、矩阵可逆性判断、几何体积计算及线性相关性判断等领域的应用。文章强调理解行列式的几何意义和灵活运用性质的重要性,并给出了学习建议和常见误区提示。行列式作为连接代数与几何的桥

aichitang2024的博客 3044

PAT——1114 全素日

以上图片来自新浪微博,展示了一个非常酷的“全素日”:2019年5月23日。即不仅20190523本身是个素数,它的任何以末尾数字3结尾的子串都是素数。本题就请你写个程序判断一个给定日期是否是“全素日”。

Jamaple的博客 760

vesta软件_分子动力学计算实战:利用 OVITO 软件找弗伦克尔缺陷和位错环分析

今天继续和大家分享这篇纯分子动力学(MD)的 Nat. Comm. 文章(DOI:10.1038/s41467-018-07102-3)。上期推文(从一篇 Nat. Comm. 学习经典分子动力学基本知识和相关软件基本操作 (一)),我们借用该文章的 fig.1 简单介绍了如何用文中所提到的 LAMMPS 软件来实现材料中级联碰撞过程。但是,想完整的展示类似该文章的 fig.1,单纯利用...

weixin_39524842的博客 5988

PAT【甲级】1114

题目链接:PAT【甲级】1114 题目简述:给定每个⼈人的家庭成员和其⾃自⼰己名下的房产,要求统计出每个家庭的⼈人⼝口数、⼈人均房产⾯积及房产套数。家庭信息⾸先按⼈均⾯积降序输出,若有并列,则按成员编号的升序输出。 #include<bits/stdc++.h> using namespace std; unordered_map<int, int> areas, sets, members; int father[10000], exist[10000], minID[10000

qq_41375322的博客 230

(project generation have a problem)STM32CubeMX生成工程一直报错项目生成有问题[玄学三步解]

解决办法 第一步,检查自己是不是真的配置的不对,JAVA安装没,pack路径设置的对不对,这里有一位前辈的博文写的非常好,附加链接https://blog.csdn.net/ybhuangfugui/article/details/52225736#commentBox 第二步,如果按照前辈的博文设置的都一模一样,还是不对,想我下图这样。 那你来对地方了 方法很简单,你把你生成工程文件的路径彻底的...

展桑博客-BenjieBlog 2929

SQL2000在Win10上安装完整指南:兼容模式、SP4补丁与远程连接配置

数据库兼容性是老系统迁移与运维中绕不开的难题。当历史遗留的SQL Server 2000遭遇全新的Windows 10环境,安装程序常因系统版本检测失败而拒绝运行,这背后是旧版安装程序与NT内核版本间的识别错位。通过兼容模式模拟旧系统环境、以管理员身份运行安装程序,并处理注册表挂起与杀毒软件干扰,即可突破基础安装障碍。安装后的SP4补丁与TCP/IP端口配置,则是保障服务稳定与远程连接的关键。这一技术路径广泛适用于制造企业MES、进销存系统及医院HIS等仍依赖老旧数据库的场景,为IT运维与数据迁移测试提供了

weixin_29266679的博客 508

1114. Family Property (25)

1114. Family Property (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to help us col

你最终的朋友的博客 839

基于SHIS凝视型短波红外高光谱相机(900-1700nm)区分不同宝石的差异性和可分性(宝石分选)

SHIS系列凝视型高光谱相机基于液晶可调谐滤波器(LCTF),通过电控LCTF进行连续细分窄波段分光,对每个波段进行高分辨率的二维面阵成像,进行图像处理后,获得完整的高光谱高清图像序列。具有低功耗、小型化、使用方便和易于扩展的优势。其工作波段覆盖400nm至1000nm,能够捕捉宝石在不同波长下的光谱特征,为宝石鉴定提供了新的工具。

wayho的博客 716

*浙大PAT甲级 1114 并查集

考察并查集,并附加记录额外信息。 为了得到一个家族中的最小id,我们可以将最小的id做为这个家族的根。并将整个家族的信息如家族人数,家族房产数,家族的房产面积。 然后通过排序,输出相应结果。 AC代码: #include #include #include #include #include #include #include #include #include #include #inc

威的博客 1099

低功耗蓝牙技术 > GAP和GATT介绍

GAP和GATT作为蓝牙协议栈中的关键协议,为BLE设备之间的通信提供了标准和框架。GAP主要负责设备的连接、广播和角色定义,而GATT则定义了BLE设备之间的具体通信方式,通过Services和Characteristics实现数据的传输和交换。在蓝牙技术的发展中,GAP和GATT两个协议扮演着关键的角色,为BLE(低功耗蓝牙)设备之间的通信提供了规范和框架。GATT连接是建立在GAP协议之上的,是一种独占连接,一个BLE外设同时只能连接一个中心设备。连接建立后,通信是双向的,不同于GAP的单向通信。

DL_62532的专栏 4043

9篇3章7节:基于 R 语言的 FAERS 数据提取与合并

本文主要探讨了 FAERS 数据提取与合并的操作流程。首先,通过程序化方法从本地存储的 FAERS ASCII 文件中提取指定季度或范围内的单药报告数据,并可通过参数设置控制数据存储位置、处理方式、计算资源分配以及报告者职业类别等。随后,通过批量读取的方式对多个季度的 DRUG 文件进行循环处理,并将其合并为一个综合数据框。

MD Code 1276

PAT 1114

PAT 1114 C++版 1.题意 1.1 题目链接 https://pintia.cn/problem-sets/994805342720868352/problems/994805356599820288 1.2 题意 这次,你需要帮助我们计算出家庭财产数据。给出每个人的家庭成员,以及他的房产信息,我们需要知道每个家庭的大小,以及平均大小以及它们房产的套数。 输入:ID Father Mo...

说文科技,做有态度的研究。 738

python 实现一个完整的基于Python的多视角三维重建系统,包含特征提取与匹配、相机位姿估计、三维重建、优化和可视化等功能

本文介绍了一个基于Python的多视角三维重建系统的实现,涵盖了特征提取与匹配、相机位姿估计、三维重建、优化和可视化等功能。首先,系统通过安装必要的库(如OpenCV、NumPy等)并加载图像数据,初始化相机参数。接着,系统使用SIFT、SURF或ORB算法提取图像特征点,并通过FLANN或BF方法进行特征匹配,应用Lowe's ratio test筛选优质匹配。该系统为后续的相机位姿估计和三维重建奠定了基础,能够从多视角图像中恢复三维场景结构。

huanghm88的专栏 623

PAT 1114 Family Property

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

不忘初心 538

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

pat A1114

网上找了一篇博客,链接给出https://blog.csdn.net/jolivan/article/details/59481949 但它存在一个问题:拥有共同孩子的两个人没有归到同一个家庭 自己动手修改后: //pat A1114 Family Property(25 分) #include &lt;cstdio&gt; #include &lt;algorithm&gt; #incl...

qq_38705851的博客 418

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

PAT 甲级 1114  Family Property

1114 Family Property (25 point(s)) 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...

漫浸天空的雨色的备忘录~ 722

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

PAT 1114 Family Property(dfs)

题目链接: 1114 Family Property 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...

机器学习,数据挖掘算法爱好者 334

PAT 1114. Family Property (并查集)

总结了一下PAT的题目,我们要静下心来,耐心的实现每一个细节,代码实现起来比较不会出bug 并查集,我们可以一次一次完成,不要想着一部到位!!! This time, you are supposed to help us collect the data for family-owned property. Given each person's family

AK_HuangYC的博客 836

PAT甲级1114 【Family Property】 (25)

之前天梯赛选拔的时候做过这题,中文,当时对于并查集掌握的很差,看了题解才写出来。这次由于很久没接触并查集,很多东西都忘了,靠记忆拼出了一部分代码,果然只过了一个点,找了一晚上的错误,原来是计数计漏了。。。。。 #include&lt;stdio.h&gt; #include&lt;vector&gt; #include&lt;algorithm&gt; #include&lt;string.h...

gq__97的博客 413

1114. Family Property (25)-PAT甲级真题(并查集)

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 ...

柳婼 の blog 5461

PAT 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 f...

Hickey_Chen的博客 375

PAT A1114

#include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;vector&gt; #include &lt;queue&gt; using namespace std; const int maxn=10010; struct people{ int numset,area; vector&lt;int&gt; relat...

weixin_37565706的博客 313

pat-1114

#include<iostream> #include<algorithm> #include<vector> using namespace std; int fa[10000]={0},exist[10000]={0}; int bookminroot[10000]={0}; int bookk[10000]={0}; int booknum[10000]={0}; double bookvs[10000],bookva[10000]; struct condi{ .

江船夜雨听笛的博客 131

PAT甲级——A1114 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 e...

weixin_30632089的博客 150

PAT A1114题目记录。以及出错的地方

我在初始化的地方错了,后面找了好久才找出来。呜呜呜 #include <iostream> #include <algorithm> #include <cstdio> #include <stdio.h> #include <math.h> #include <queue> using namespace std; int ...

weixin_44980441的博客 638

PAT - 甲级 - 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 f

i逆天耗子 725
上一篇: hdu4717 The Moving Points 三分求函数最小值
下一篇: hdu4278 Faulty Odometer 八进制转十进制
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值