【无标题】

这篇博客介绍了一种使用树形动态规划(Tree DP)解决计算机网络中信号传输最大距离的问题。作者提供了具体的数据结构和算法实现,包括二次换根和计算树的直径。示例输入和输出展示了算法的运行情况,代码中包含了详细的DFS遍历过程。
摘要由CSDN通过智能技术生成

F - Computer

 HDU - 2196 

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

InputcopyOutputcopy
 
5 1 1 2 1 3 1 1 1 
 
3 2 3 4 4 

思路:树形dp二次换根问题,同时还需要学树形dp直径问题

代码:

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e4+10;
struct node
{
    int to, ne, w;
}edge[maxn * 2];
int head[maxn];
int dp[maxn][4];
int cnt;
int n;
void add(int u,int v,int w)
{
    edge[++cnt].ne = head[u];
    edge[cnt].to = v;
    edge[cnt].w = w;
    head[u] = cnt;
}
void dfs1(int u,int fa)
{
    for(int i = head[u]; i; i = edge[i].ne)
    {
        int v = edge[i].to;
        if(v == fa)
            continue;
        dfs1(v, u);
        int tp = dp[v][0] + edge[i].w;
        if(dp[u][0] <= tp)
        {
            dp[u][1] = dp[u][0];
            dp[u][0] = tp;
        }
        else if(dp[u][1] < tp)
        {
            dp[u][1] = tp;
        }
    }
}
void dfs2(int u,int fa)
{
    for(int i =head[u]; i; i=edge[i].ne)
    {
        int v = edge[i].to;
        if(v == fa)
            continue;
        if(dp[u][0] == dp[v][0] + edge[i].w)
        {
            dp[v][2] = max(dp[u][1],dp[u][2]) + edge[i].w;
        }
        else
            dp[v][2] = max(dp[u][0],dp[u][2]) + edge[i].w;
        dfs2(v,u);
    }

}
int main()
{
    while(cin >> n)
    {
        memset(dp,0,sizeof dp);
        memset(head,0,sizeof head);
        memset(edge,0,sizeof edge);
        cnt = 0;
        int v, w;
        for(int i = 2; i <= n; i++)
        {
            cin >> v >> w;
            add(i, v, w);
            add(v, i, w);
        }
        dfs1(1,-1);
        dfs2(1,-1);
        for(int i = 1; i <= n; i++)
        {
            cout << max(dp[i][0], dp[i][2]) << endl;
            //cout<<dp[i][0]<<endl;
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值