hdu2196--Computer(樹形DP練習2)

來源:互聯網
上載者:User

標籤:

Computer Time Limit:1000MS      Memory Limit:32768KB      64bit IO Format:%I64d & %I64uSubmit Status

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

 51 12 13 11 1 
 

Sample Output

 32344 


給出電腦之間串連的關係,和相互之間的長度,求每台電腦到最遠的一個的距離是多少。

對於一個點i來說,i能到的最遠距離可能有兩種情況:

1.由i走向i的子節點存在最遠長度。(向下)

2.由i走向i的父節點存在最遠長度。 (向上)

dp[i][0]記錄節點i向下的最長距離,belong[i][0]記錄向下最長路徑經過的i的子節點。

dp[i][0]記錄節點i向下的第二長距離,belong[i][0]記錄向下第二長路徑經過的i的子節點。

flag[i] = 0代表i的父節點的最長路徑不經過i, = 1 代表i的父節點的最長路徑經過i

狀態轉移方程:先由dfs,找出所有點向下的距離,然後由上向下層次遍曆

設 i是j的父節點

如果節點j的flag[j] = 0 ,那麼它的最遠距離應該是dp[j][0] 和 i的最遠距離+i到j的距離 中的最大值。

如果節點j的flag[j] = 1 ,那麼它的最遠距離應該是 dp[j][0] 和 i的非包含j的路徑的距離+i到j的距離 中的最大值。

注意:隨時更新dp[i][0] dp[i][1]的值。


#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std ;struct node{    int u , v , w ;    int next ;}edge[11000];int head[11000] , cnt ;int dp[11000][2] , flag[11000] , belong[11000][2];queue <int> que ;void add(int u,int v,int w){    edge[cnt].u = u ; edge[cnt].v = v ;    edge[cnt].w = w ;    edge[cnt].next = head[u] ;    head[u] = cnt++ ;}void dfs(int u){    if( head[u] == -1 )        return ;    int i , v , k1 , k2 ;    for( i = head[u] ; i != -1 ; i = edge[i].next )    {        v = edge[i].v ;        dfs(v) ;        if( dp[v][0]+edge[i].w > dp[u][0] )        {            dp[u][1] = dp[u][0] ; belong[u][1] = belong[u][0] ;            dp[u][0] = dp[v][0]+edge[i].w ; belong[u][0] = v ;        }        else if( dp[v][0]+edge[i].w > dp[u][1] )        {            dp[u][1] = dp[v][0]+edge[i].w ; belong[u][1] = v ;        }    }}int main(){    int n , u , v , w ;    int i ;    while( scanf("%d", &n) != EOF )    {        memset(flag,0,sizeof(flag)) ;        memset(dp,0,sizeof(dp)) ;        memset(head,-1,sizeof(head)) ;        memset(belong,0,sizeof(belong)) ;        cnt = 0 ;        add(0,1,0) ;        for(i = 2 ; i <= n ; i++)        {            scanf("%d %d", &u, &w) ;            add(u,i,w) ;        }        dfs(1) ;        while( !que.empty() ) que.pop() ;        que.push(0) ;        while( !que.empty() )        {            u = que.front() ;            que.pop() ;            for(i = head[u] ; i != -1 ; i = edge[i].next)            {                v = edge[i].v ;                if( flag[v] )                {                    if( dp[v][0] + edge[i].w == dp[u][0] )                    {                        if( dp[u][1]+edge[i].w > dp[v][0] )                        {                            flag[ belong[v][0] ] = 1;                            dp[v][1] = dp[v][0] ;                            dp[v][0] = dp[u][1]+edge[i].w ;                        }                        else if( dp[u][1] + edge[i].w > dp[v][1] )                        {                            flag[ belong[v][0] ] = 1 ;                            dp[v][1] = dp[u][1] + edge[i].w ;                        }                        else                        {                            flag[ belong[v][0] ] = 1 ;                            flag[ belong[v][1] ] = 1 ;                        }                    }                    else                    {                        if( dp[u][0]+edge[i].w > dp[v][0] )                        {                            flag[ belong[v][0] ] = 1;                            dp[v][1] = dp[v][0] ;                            dp[v][0] = dp[u][0]+edge[i].w ;                        }                        else if( dp[u][0] + edge[i].w > dp[v][1] )                        {                            flag[ belong[v][0] ] = 1 ;                            dp[v][1] = dp[u][0] + edge[i].w ;                        }                        else                        {                            flag[ belong[v][0] ] = 1 ;                            flag[ belong[v][1] ] = 1 ;                        }                    }                }                else                {                    if( dp[u][0]+edge[i].w > dp[v][0] )                    {                        flag[ belong[v][0] ] = 1;                        dp[v][1] = dp[v][0] ;                        dp[v][0] = dp[u][0]+edge[i].w ;                    }                    else if( dp[u][0] + edge[i].w > dp[v][1] )                    {                        flag[ belong[v][0] ] = 1 ;                        dp[v][1] = dp[u][0] + edge[i].w ;                    }                    else                    {                        flag[ belong[v][0] ] = 1 ;                        flag[ belong[v][1] ] = 1 ;                    }                }                que.push(v) ;            }        }        for(i = 1 ; i <= n ; i++)            printf("%d\n", dp[i][0]) ;    }    return 0;}


hdu2196--Computer(樹形DP練習2)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.