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

从零开始的机械臂yolov5抓取gazebo仿真(一) 本文介绍了如何将机械臂模型导出为URDF 阅读详情

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;
}




在Allegro设计界面中如何修改封装焊盘 在高密度板的情况下,SUIPLA(支持内层没有的焊盘摘掉)打勾,在高密度板的情况下,内层没有的焊盘摘掉可以增加走线的空间。3、修改完焊盘后,选择File→Update to Design(更新到设计)。然后关闭焊盘修改对话框即可。,将会定期不定期更新技术文章,互相学习交流。或者点击Options选项卡下要修改的焊盘,然后点击Edit修改。在Allegro PCB设计界面中修改封装焊盘的方法。2、然后点击封装上要修改的焊盘,右击选择Edit。修改完成后如下图所示。 阅读详情

相关推荐

PCI-Express、MiniPCIe、ExpressCard接口转换说明/引脚互换关系

通过引脚PIN互换可以在PCI-Express、MiniPCIe、ExpressCard之间切换不同的接口;提供接口引脚表和标准尺寸图。

tech34的博客 7143

PAT——1114 全素日

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

Jamaple的博客 761

【TM1650】STM8S TM1650芯片控制程序

以下为.h文件: #ifndef __TM1650_I2C_H #define __TM1650_I2C_H #include "stm8s.h" #include "stm8s_gpio.h" #include "tim1.h" #include "usart2.h" #include &lt;iostm8s103f3.h&gt; #include &lt;intrinsics....

cxw312864660的博客 1万+

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的博客 231

从Pikachu靶场实战解析越权漏洞:原理、攻击与防御

本文通过Pikachu靶场实战详细解析越权漏洞的原理、攻击方式与防御策略。文章深入探讨水平越权和垂直越权的危害,结合代码示例展示漏洞利用过程,并提供代码层和架构层的立体化防御方案,帮助开发者有效防范这一Web安全隐形杀手。

weixin_29325007的博客 369

从零开始安装 Windows 11 和 Ubuntu 22.04 双系统

本文详解Windows与Ubuntu 22.04双系统安装流程:需提前确认BIOS模式(UEFI/GPT或Legacy/MBR),关闭BitLocker加密、设备加密及快速启动;推荐Rufus/Ventoy制作启动盘。安装时需规划分区方案(建议单独划分EFI系统分区),注意引导器安装位置选择。双系统启动通过BIOS引导顺序管理,安装后需同步Windows与Linux系统时间。教程适配主流UEFI+GPT设备,强调操作前数据备份及Windows更新策略调整,避免兼容性问题。

萑澈的博客 3812

*浙大PAT甲级 1114 并查集

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

威的博客 1100

跟着团子学SAP PS:WBS BOM的实际应用价值 CS71/CKW1/CKW4

过往在参与很多项目实施的时候有很多顾问都会提到WBS BOM是否要启用,但仔细聊下来会发现大部分人对WBS BOM的真正功能以及实际应用场景并不是非常了解,因为WBS BOM的实际应用场景非常有限,大部分情况下基本都可以不用考虑启用WBS BOM。 WBS BOM和普通BOM的核心区别是多了一个WBS元素编码的维度,也就是说同一个成品料号可以基于不同WBS元素编码设计不同的BOM主数据。 所以从本质而言WBS BOM真正解决的问题只是减少成品物料的物料编码,比如一个项目中生产2个同型号的控制柜,但两个控制柜

Lucas1211的博客 6328

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

YOLO26优化:AAAI2026 | 融合PartialNet Block的C3k2-YOLO高效目标检测网络 | 轻量化涨点设计

PartialNet Block 结合YOLO26的C3k2模块,进行二次创新。

①订阅者答疑群聊服务;②FDE:工业缺陷检测项目;③paper/创新算法指导;④免费提供YOLO创新涨点推荐顾问Skill和AI小怪兽数字分身; 621

BES2300x笔记(36) -- OTA升级

哈喽大家好,这是该系列博文的第三十六篇~ 篇~ <<【系列博文索引】快速通道 >> 一、前言 BES 蓝牙方案自带 OTA(Over The Air)功能,该功能可以对蓝牙固件进行在线升级,目前该功能支持通过安卓手机(苹果手机也有对应APP,未实际测试)对蓝牙固件进行升级,以后还会支持通过 PC 端进行在线升级。在使用 OTA 功能之前,用户需安装 BES 提供的手机 apk 软件,将需要升级的固件拷贝到手机,然后通过该 apk 软件进行在线升级。

文化人的博客 5367

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

TripoSR: Fast 3D Object Reconstruction from a Single Image 论文阅读

模型的主要参数,如变换器的层数、三平面的维度、NeRF模型的具体细节和主要训练配置,在表1中详细说明。TripoSR的算法流程结合了先进的数据处理、模型设计和训练技术,使其能够在不到0.5秒的时间内从单张图像中快速生成详细的3D网格。总体而言,TripoSR是一个强大的3D重建工具,它在速度和质量上都表现出色,但也需要考虑到其对计算资源的需求和在处理极端复杂场景时可能遇到的挑战。:通过精心策划的训练数据集和多样化的数据渲染技术,TripoSR提高了模型的泛化能力和对真实世界图像的适应性。

Christo的博客 1957

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

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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

Mr贱的博客 204

PAT 1114

#include #include #include #include #include #include #include using namespace std; const int maxn = 10100; int e[maxn] = { 0 }, a[maxn] = {0}; int id, fa, mo, k, child,estate,area; int n; int father[

好好学习,天天向上 419

PAT甲级1114 【Family Property】 (25)

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

gq__97的博客 414

pat甲级1114

#include<stdio.h> #include<algorithm> using namespace std; const int N=10000; int father[N]; int count1[N]={0}; //记录不同家庭人员数目 bool count2[N]={0}; //记录id是否有效 struct node{ int id; ...

代洋 202

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

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

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

1114. Family Property (25)(PAT 并查集)

题目地址https://www.patest.cn/contests/pat-a-practise/1114ac注意 主要是并查集,不能漏掉 int fa[N];int find(int x) { if(x == fa[x]) return x; return fa[x] = find(fa[x]); }void merg(int x, int y) { int xx = fin

master-dragon的专栏 973

PAT A1114 family property (25)

//用到并查集,有关系的人在一个圈子里,合并的时候注意根设为id较小的那个。遍历统计root和计房产数和面积,我单独开了一个数组放rootid #include #include #include #include using namespace std; int n; int father[10010]; int isroot[10010]; int familynum[10010]={0};

momottyy的专栏 411

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
上一篇: 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、付费专栏及课程。

余额充值
⌂ 换网址