PAT_1090. Highest Price in Supply Chain

本文介绍了一种使用C++实现的树形结构深度遍历算法。通过优化搜索过程,利用标记叶节点和缓存已知深度的方式显著提高了效率。

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

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int N,maxDepth=0,num=0;
double P,r;
struct Node
{
	int father,depth;
}node[100005];
bool isYezi[100005];
int Find(int v)
{

	int depth=0;
	int x=v;
	while(node[x].father!=-1)
	{
		if(node[x].depth!=-1)
		{
			//cout<<"第"<<x<<"节点的depth="<<node[x].depth<<endl;
			return depth+node[x].depth-1;
		}
		x=node[x].father;
		depth++;
	}
	int temp=depth;
	while(v!=-1)
	{
		v=node[v].father;
		node[v].depth=temp--;
		//cout<<"node["<<v<<"].depth="<<temp+1<<endl;
	}
	return depth;
}
int main()
{
	for(int i=0;i<100005;i++)
		node[i].depth=-1;
	memset(isYezi,true,sizeof(isYezi));
	scanf("%d%lf%lf",&N,&P,&r);
	for(int i=0;i<N;i++)
	{
		scanf("%d",&node[i].father);
		if(node[i].father!=-1)
			isYezi[node[i].father]=false;
	}
	for(int i=0;i<N;i++)
	{
		if(isYezi[i]==true)
		{
			int depth=Find(i);
			if(maxDepth<depth)
			{
				num=1;
				maxDepth=depth;
			}
			else if(maxDepth==depth)
				num++;
		}
	}
	printf("%.2lf %d\n",P*pow(r/100+1, maxDepth),num);
	return 0;
}
			

  

自己的思路做起来比较麻烦,最初的想法代码实现起来非常快,40行就解决了,用一个father数组来记录每个节点的父亲节点,因为这是一棵树,所以我们每次只要搜索某个节点的父亲节点,一直往上面搜,找到根节点结束,用depth来记录这次搜索的长度即可,但仅仅是这样的思路,会有四个点超时。

思考:其实我们做了很多不必要的搜索,比如非叶节点,一定不能得到最长的路径,所以非叶节点就不应该开始搜索,此时我们需要加入一个bool 数组isYezi来保存某个节点是否为叶子节点。优化之后还是有一个节点超时,那其实当我们对一个叶子结点进行搜索之后,这条路径上的节点的深度都已经被计算出来了,当下次对另外的叶子节点进行搜索时若再搜到这个点时,就不需要再往上搜索了,直接加上这个节点的深度即可。

经过两次优化,就可以通过所有的测试点了。

以下信息能否看出静态内存占用大小? Build started: Project: Project *** Using Compiler 'V6.21', folder: 'D:\Keil_v5\ARM\ARMCLANG\Bin' Build target 'GD32L23x' compiling std_i2c.c... compiling systick.c... compiling main.c... compiling dev_rx8010dj.c... compiling gd32l23x_it.c... compiling charge_sc8815.c... compiling std_fmc.c... compiling std_adc.c... compiling std_led.c... compiling heat_sy8366a.c... compiling std_pwm.c... compiling battery_manage.c... compiling std_stdio.c... compiling std_time.c... compiling std_uart.c... compiling std_log.c... compiling std_upgrade.c... compiling std_fwdgt.c... compiling gd32l23x_dac.c... compiling system_gd32l23x.c... compiling std_485net.c... compiling gd32l23x_exti.c... compiling gd32l23x_dma.c... compiling gd32l23x_gpio.c... compiling gd32l23x_misc.c... compiling gd32l23x_rcu.c... compiling gd32l23x_syscfg.c... compiling gd32l23x_usart.c... compiling gd32l23x_cau.c... compiling gd32l23x_cau_des.c... compiling gd32l23x_cau_tdes.c... compiling gd32l23x_dbg.c... compiling gd32l23x_fwdgt.c... compiling gd32l23x_cau_aes.c... compiling gd32l23x_vref.c... compiling gd32l23x_rtc.c... compiling gd32l23x_adc.c... compiling gd32l23x_cmp.c... compiling gd32l23x_crc.c... compiling gd32l23x_fmc.c... compiling gd32l23x_i2c.c... assembling startup_gd32l23x.s... compiling gd32l23x_lpuart.c... compiling gd32l23x_pmu.c... compiling gd32l23x_spi.c... compiling gd32l23x_trng.c... compiling gd32l23x_wwdgt.c... compiling gd32l23x_timer.c... compiling gd32l23x_ctc.c... compiling gd32l23x_lptimer.c... compiling gd32l23x_slcd.c... compiling gd32l233r_eval.c... linking... Program Size: Code=46794 RO-data=13362 RW-data=1328 ZI-data=2736 FromELF: creating hex file... After Build - User command #1: ..\Objects\hex_crc.bat hex2bin v2.5, Copyright (C) 2017 Jacques Pelletier & contributors Allocate_Memory_and_Rewind: Lowest address: 0800C000 Highest address: 0801AC2F Starting address: 0800C000 Max Length: 60464 Binary file start = 0800C000 Records start = 0800C000 Highest address = 0801AC2F Pad Byte = FF ..\Objects\App.bin Checksum: 0xD6 "..\Objects\App.axf" - 0 Error(s), 0 Warning(s). Build Time Elapsed: 00:00:30
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值