Computer (树形dp 经典题)

题目: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
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-1台电脑,给出连接状态,即给出了一颗树,求每个电脑能到达的最远距离是多少,即求树上的每个点能到达的最远的距离是多少。

输入:

第一个数n,表示有n个节点,后面n-1行,每行输入的是第i台电脑和第几台电脑相连,边权是多少,即第二行1 1表示第二台电脑和第一台电脑相连,距离是1。

思路:

这个题刚上来是没有思路的……甚至连题都看不懂。这个也是看的时间最长的一个题吧。

用struct建树,struct Tree(node)表示树上的每一个点。

这个题需要两个搜索。因为对于一个节点来说,它的最大距离可能来自于它的子树,另一个是来自于它的父节点。所以需要两个搜索。

第一次搜索求出任一节点在它的子树范围内到达叶子节点的最大距离和次大距离。为什么要求次大距离呢?因为如果只存最大值的话,判断其从父节点过来的最大值时,如果其父节点存的最大值刚好是从该点过来的,那么就失去了父节点过来的状态。第二次搜索就是从父节点过来的最大值。

head数组表示头结点,建树时需要。

源代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#include <cmath>
using namespace std;
struct Tree{
    int from,next,length;
}tree[100005];
int head[1000005];//头节点
int max_up_to_down[100005];//从该节点往下搜索得到的最大距离
int max_up_to_down_2[1000005];//从该节点往下搜索的次大距离
int maxid[1000005];//最大距离对应的序号
int maxid2[1000005];//次大距离对应的的序号
int num;
void build(int i,int con,int length){//构建树
    tree[num].from = con;
    tree[num].length = length;
    tree[num].next = head[i];
    head[i] = num++;
    tree[num].from = i;
    tree[num].length = length;
    tree[num].next = head[con];
    head[con] = num++;
}
//求节点v向下到叶子节点的最大距离
void tree_dp1(int v,int father){
    max_up_to_down[v] = 0;
    max_up_to_down_2[v] = 0;
    for (int i = head[v]; i != -1; i = tree[i].next){
        int v1 = tree[i].from;
        if (v1 == father)
            continue;
        tree_dp1(v1,v);
        if (max_up_to_down_2[v] < max_up_to_down[v1] + tree[i].length){
            max_up_to_down_2[v] = max_up_to_down[v1] + tree[i].length;
            maxid2[v] = v1;
            if (max_up_to_down_2[v] > max_up_to_down[v]){
                swap(max_up_to_down[v],max_up_to_down_2[v]);
                swap(maxid[v],maxid2[v]);
            }
        }
    }
}
//求父节点过来的长度
void tree_dp2(int v,int father){
    for (int i = head[v]; i != -1; i = tree[i].next){
        int v1 = tree[i].from;
        if (v1 == father)
            continue;
        if (v1 == maxid[v]){
            if (max_up_to_down_2[v1] < tree[i].length + max_up_to_down_2[v]){
                max_up_to_down_2[v1] = tree[i].length + max_up_to_down_2[v];
                maxid2[v1] = v;
                if (max_up_to_down_2[v1] > max_up_to_down[v1]){
                    swap(max_up_to_down[v1],max_up_to_down_2[v1]);
                    swap(maxid2[v1],maxid[v1]);
                }
            }
        }
        else {
            if (max_up_to_down_2[v1] < tree[i].length + max_up_to_down[v]){
                max_up_to_down_2[v1] = tree[i].length + max_up_to_down[v];
                maxid2[v1] = v;
                if (max_up_to_down_2[v1] > max_up_to_down[v1]){
                    if (max_up_to_down_2[v1] > max_up_to_down[v1]){
                        swap(max_up_to_down[v1],max_up_to_down_2[v1]);
                        swap(maxid2[v1],maxid[v1]);
                    }
                }
            }
        }
        tree_dp2(v1,v);
    }
}
int main(){
    int n;
    while (scanf("%d",&n)!=EOF){
        num = 0;
        memset(head,-1,sizeof(head));
        int con,length;//和哪一台连接,以及长度。
        for (int i = 2; i <= n; i++){
            scanf("%d%d",&con,&length);
            build(i,con,length);
        }
        tree_dp1(1,-1);
        tree_dp2(1,-1);
        for (int i = 1; i <= n; i++)
            printf("%d\n",max_up_to_down[i]);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值