Computer

题目:

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

输出:

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台电脑,电脑的连接是一个树的结构,让你求这棵树每个节点的最远距离节点。

解题思路:

 

 

如图,先自底向上保存每个节点到子节点的最大距离。

然后用它的兄弟节点更新这个最大距离。

子树距离最大的状态转移方程为

dp1[u] =max(dp1[v] + u_v.weight);——————①

向上再向下到兄弟节点的状态转移方程为

dp2[v] = max(dp2[v], dp1[v_brother]+u_v.weight+u_vbrother.weight)——————②

一直向上到某个点才向下的状态转移方程为

dp2[v] = max(dp2[v],dp2[u]+u_v.weight)——————③

通过这三个状态转移方程,我们就能求得每个节点的距离最大值了

①需要一次自底向上的转移,②,③需要一次自顶向下的转移

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 10005;

long long dp1[maxn];   //从该结点往下走距离最大为多少
long long dp2[maxn];   //从该结点往上走距离最大为多少

struct node
{
    int to, nxt;
    long long weight;
} edges[maxn<<2];

int head[maxn<<2];
int tot;

void addEdge(int u, int v, long long w)
{
    edges[tot] = node{v,head[u], w};
    head[u] = tot++;
}

void dfs1(int u, int fa)
{
    dp1[u] = 0;
    for(int i = head[u]; ~i; i = edges[i].nxt)
    {
        int v = edges[i].to;
        long long w = edges[i].weight;
        if(v == fa)
            continue;
//        cout << u<<" "<< v<<endl;
        dfs1(v,u);                               //所谓自底向上,就是先dfs,再状态转移
        dp1[u] = max(dp1[u],dp1[v]+w);
    }
}

void dfs2(int u, int fa, int fafa)  //fafa为父亲的父亲
{
    long long u_v_weight = 0;
    dp2[u] = 0;
    for(int i = head[fa]; ~i; i = edges[i].nxt)
    {
        int v = edges[i].to;
        long long w = edges[i].weight;
        if(v == u)
            u_v_weight = w;
        else if(v == fafa)
            continue;
        else{
            dp2[u] = max(dp2[u],dp1[v]+w);         //从兄弟节点向下走
        }
    }
    dp2[u] = max(dp2[u],dp2[fa]);  //虽然是向上,不从兄弟节点走,而是继续向上走。
    dp2[u]+=u_v_weight;
                                                //所谓自顶向下,就是先转移,再dfs
    for(int i = head[u]; ~i; i = edges[i].nxt)
    {
        int v = edges[i].to;
        if(v == fa) continue;
        dfs2(v,u,fa);
    }
}



int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        int u, v;
        long long w;
        tot = 1;
        memset(head,-1,sizeof(head));
        for(int i = 2; i <= n; i++)
        {
            scanf("%d%lld", &v, &w);
            addEdge(i,v,w);
            addEdge(v,i,w);
        }
        dfs1(1,-1);
        dfs2(1,0,-1);
        for(int i = 1; i <= n; i++)
        {
//            cout <<"## " <<i<<" "<<dp1[i]<<" "<<dp2[i]<<endl;
            printf("%lld\n", max(dp1[i],dp2[i]));
        }
    }

    return 0;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值