1. 程式人生 > >Computer (樹形dp 經典題)

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;
}