HDU - 2196 Computer 树形dp 两边dfs 典型

本文介绍了一种用于计算树形结构中每个节点到其他所有节点最大距离的算法。通过两次深度优先搜索(DFS),算法能高效地找出每个节点到最远节点的距离,适用于网络延迟分析等场景。

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

A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information. 



Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.

Input

Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.

Output

For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).

Sample Input

5
1 1
2 1
3 1
1 1

Sample Output

3
2
3
4
4   

题意:给出一颗树,求树中的每个顶点到其他所有顶点的最大值。

题解:一个节点的最大距离有两种情况:

1. 到子节点的最大距离

2.父节点的最大距离(除了该节点分支)+ 根节点到该节点的距离

所以我们两边dfs 即可解决

第一遍,我们记录下每个节点到子节点的最大距离和次大距离,并记录最大距离的子代

第二遍,更新每个节点经过父节点这个路径的最大距离

最后输出,经过父节点最大距离和到子节点最大距离较大一个即可

 

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int N=1e4+10;
vector<int> v[N];
int n,lm[N],head[N],dp[N][3],len;
struct node{
	int to,d,nex;
}e[N*2];
void add(int x,int y,int d)
{
	e[len].to=y;
	e[len].d=d;
	e[len].nex=head[x];
	head[x]=len++;
}
int dfs(int f,int u)
{
	dp[u][0]=dp[u][1]=dp[u][2]=0;
	for(int i=head[u];i!=-1;i=e[i].nex)
	{
		int to=e[i].to;
		if(to==f) continue;
		int dis=dfs(u,to)+e[i].d;
	//	cout<<dis<<endl;
		if(dis>dp[u][0]) dp[u][1]=dp[u][0],dp[u][0]=dis,lm[u]=to;
		else if(dis>dp[u][1]) dp[u][1]=dis;
	}
//	cout<<u<<" "<<dp[u][0]<<" "<<dp[u][1]<<endl;
	return dp[u][0];
}
void dfs2(int f,int u)
{
	for(int i=head[u];i!=-1;i=e[i].nex)
	{
		int to=e[i].to;
		if(to==f) continue;
		if(to==lm[u]) dp[to][2]=max(dp[u][1],dp[u][2])+e[i].d;
		else dp[to][2]=max(dp[u][0],dp[u][2])+e[i].d;
//		cout<<to<<" "<<dp[to][2]<<endl;
		dfs2(u,to);
	}
}
int main()
{
	int u,d;
	while(~scanf("%d",&n))
	{
		memset(head,-1,sizeof(head));
		len=0;
		memset(lm,-1,sizeof(lm));
		for(int i=2;i<=n;i++)
		{
			scanf("%d%d",&u,&d);
			add(u,i,d);
			add(i,u,d);	
		}
		dfs(0,1);
		dfs2(0,1);
		for(int i=1;i<=n;i++)
			printf("%d\n",max(dp[i][0],dp[i][2]));
	}
	return 0;
}

 

内容概要:本文介绍了奕斯伟科技集团基于RISC-V架构开发的EAM2011芯片及其应用研究。EAM2011是一款高性能实时控制芯片,支持160MHz主频和AI算法,符合汽车电子AEC-Q100 Grade 2和ASIL-B安全标准。文章详细描述了芯片的关键特性、配套软件开发套件(SDK)和集成开发环境(IDE),以及基于该芯片的ESWINEBP3901开发板的硬件资源和接口配置。文中提供了详细的代码示例,涵盖时钟配置、GPIO控制、ADC采样、CAN通信、PWM输出及RTOS任务创建等功能实现。此外,还介绍了硬件申领流程、技术资料获取渠道及开发建议,帮助开发者高效启动基于EAM2011芯片的开发工作。 适合人群:具备嵌入式系统开发经验的研发人员,特别是对RISC-V架构感兴趣的工程师和技术爱好者。 使用场景及目标:①了解EAM2011芯片的特性和应用场景,如智能汽车、智能家居和工业控制;②掌握基于EAM2011芯片的开发板和芯片的硬件资源和接口配置;③学习如何实现基本的外设驱动,如GPIO、ADC、CAN、PWM等;④通过RTOS任务创建示例,理解多任务处理和实时系统的实现。 其他说明:开发者可以根据实际需求扩展这些基础功能。建议优先掌握《EAM2011参考手册》中的关键外设寄存器配置方法,这对底层驱动开发至关重要。同时,注意硬件申领的时效性和替代方案,确保开发工作的顺利进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值