Hdu 2196 Computer【树型Dp】经典题

本文介绍了一种算法,用于解决给定一棵树后求每个节点到树中其它节点的最大距离的问题。通过两次深度优先搜索(DFS)实现,第一次确定每个子树中离根节点最远的节点,第二次确定非子树方向上的最远距离。

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

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7193    Accepted Submission(s): 3566


Problem Description
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

题目大意:


给你N个点的一棵树,问距离每一个点最远的点的距离。


思路:


类似:http://blog.youkuaiyun.com/mengxiang000000/article/details/75578275


①我们设定Dp【u】表示以u为根的子树中,距离点u最远距离长度。

那么有:Dp【u】=max(Dp【v】+W(u,v));


②现在对于一个点u来讲,有两个方向,一个是子树方向,一个是非子树方向。

我们搞定了子树方向,就剩下非子树方向了。

那么设定F【u】表示非子树方向到点u节点最远的长度。

对于一个点来说,如果是非子树方向就有两种递推状态的方案:

1.从父亲节点来(从根节点到当前节点这条路径的长度):F【u】=F【from】+W(from,u);

2.从当前节点的兄弟节点而来;F【u】=max(Dp【brother】+W(from,u)+W(from,v));


如果存在一个点有多个儿子的情况,从2状态转移的时间复杂度会达到O(n^2),很危险,所以我们再维护一个数组Id【u】表示以u为根的子节点中Dp【v】值最大的节点编号。同时维护从这个点u到这个最大值点v直接路径的长度(W(u,v));


过程维护一下即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[35000];
int head[15000];
int fa[15000];
int maxn[15000];
int Id[15000];
int F[15000];
int dp[15000];
int n,cont;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void dfs(int u,int from)
{
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(v==from)continue;
        else
        {
            dfs(v,u);
            if(dp[v]+w>dp[u])
            {
                dp[u]=dp[v]+w;
                Id[u]=v;
            }
        }
    }
}
void Dfs(int u,int from,int prew)
{
    fa[u]=from;
    if(from!=-1)F[u]=max(F[from]+prew,F[u]);
    if(from!=-1)
    {
        if(u==Id[from])
        {
            for(int i=head[from];i!=-1;i=e[i].next)
            {
                int v=e[i].to;
                int w=e[i].w;
                if(v==u)continue;
                if(from!=-1&&v==fa[from])continue;
                if(v==from)continue;
                F[u]=max(dp[v]+prew+w,F[u]);
            }
        }
        else
        {
            F[u]=max(F[u],dp[from]+prew);
        }
    }
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(v==from)continue;
        Dfs(v,u,w);
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        cont=0;
        memset(head,-1,sizeof(head));
        memset(F,0,sizeof(F));
        memset(dp,0,sizeof(dp));
        for(int i=2;i<=n;i++)
        {
            int faa,w;
            scanf("%d%d",&faa,&w);
            add(i,faa,w);
            add(faa,i,w);
        }
        dfs(1,-1);
        Dfs(1,-1,0);
        /*
        for(int i=1;i<=n;i++)
        {
            printf("%d ",dp[i]);
        }
        printf("\n");
        for(int i=1;i<=n;i++)
        {
            printf("%d ",F[i]);
        }
        printf("\n");*/
        for(int i=1;i<=n;i++)
        {
            printf("%d\n",max(F[i],dp[i]));
        }
    }
}



















内容概要:本文详细探讨了杯形谐波减速器的齿廓修形方法及寿命预测分析。文章首先介绍了针对柔轮与波发生器装配时出现的啮合干涉问,提出了一种柔轮齿廓修形方法。通过有限元法装配仿真确定修形量,并对修形后的柔轮进行装配和运转有限元分析。基于Miner线性疲劳理论,使用Fe-safe软件预测柔轮寿命。结果显示,修形后柔轮装配最大应力从962.2 MPa降至532.7 MPa,负载运转应力为609.9 MPa,解决了啮合干涉问,柔轮寿命循环次数达到4.28×10⁶次。此外,文中还提供了详细的Python代码实现及ANSYS APDL脚本,用于柔轮变形分析、齿廓修形设计、有限元验证和疲劳寿命预测。 适合人群:机械工程领域的研究人员、工程师,尤其是从事精密传动系统设计和分析的专业人士。 使用场景及目标:①解决杯形谐波减速器中柔轮与波发生器装配时的啮合干涉问;②通过优化齿廓修形提高柔轮的力学性能和使用寿命;③利用有限元分析和疲劳寿命预测技术评估修形效果,确保设计方案的可靠性和可行性。 阅读建议:本文涉及大量有限元分析和疲劳寿命预测的具体实现细节,建议读者具备一定的机械工程基础知识和有限元分析经验。同时,读者可以通过提供的Python代码和ANSYS APDL脚本进行实际操作和验证,加深对修形方法和技术路线的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值