hdu 2196 Computer 树形dp模板题

 

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2850    Accepted Submission(s): 1450

Problem 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. 
hdu 2196 Computer 树形dp模板题


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
代码:
  1 /***

  2 分析:以编号的i的节点为例(非根节点),最长的路径长度只有俩种可能,

  3     1)子树中存在最长路径;

  4     2)通过父节点的路径中存在最长路径

  5 所以,只有分别求出每一节点对应的那俩种路径取大最大值即可,其中,根节点只存在第一种可能

  6 ***/

  7 #include "stdio.h"  

  8 #include "string.h"

  9 

 10 #define N 10005

 11 

 12 struct node{

 13     int x,y;

 14     int weight;

 15     int next;

 16 }edge[4*N];

 17 int idx,head[N];

 18 

 19 void Init(){idx=0; memset(head,-1,sizeof(head));}

 20 

 21 void Add(int x,int y,int weight)

 22 {

 23     edge[idx].x = x;

 24     edge[idx].y = y;

 25     edge[idx].weight = weight;

 26     edge[idx].next = head[x];

 27     head[x] = idx++;

 28 }

 29 

 30 struct point{

 31     int id;

 32     int value;

 33 }dp1[N],dp2[N];  //dp1[i]记录点i的最远距离,dp2[i]记录点i的次远距离,

 34 

 35 void swap(point &a,point &b)

 36 {

 37     point c;

 38     c = a;

 39     a = b;

 40     b = c;

 41 }

 42 

 43 void DFS1(int x,int father)

 44 {

 45     int i,y;

 46     dp1[x].value = dp2[x].value = 0;

 47     for(i=head[x]; i!=-1; i=edge[i].next)

 48     {

 49         y = edge[i].y;

 50         if(y==father) continue;

 51         DFS1(y,x);

 52         if(dp1[y].value + edge[i].weight > dp2[x].value)

 53         {

 54             dp2[x].value = dp1[y].value + edge[i].weight;

 55             dp2[x].id = y;

 56             if(dp1[x].value < dp2[x].value)  //dp1[i]记录点i的最远距离,dp2[i]记录点i的次远距离,

 57                 swap(dp1[x],dp2[x]);

 58         }

 59     }

 60 }

 61 

 62 void DFS2(int x,int father)

 63 {

 64     int i,y;

 65     for(i=head[x]; i!=-1; i=edge[i].next)

 66     {

 67         y = edge[i].y;

 68         if(y==father) continue;

 69         if(dp1[x].id == y) //点y是父亲x的最远距离的下一个节点

 70         {

 71             if(dp2[y].value < dp2[x].value+edge[i].weight) //,那么看点y的次元距离能否通过父亲x的其他节点更新

 72             {

 73                 dp2[y].value = dp2[x].value + edge[i].weight;

 74                 dp2[y].id = x;

 75                 if(dp1[y].value < dp2[y].value)

 76                     swap(dp1[y],dp2[y]);

 77             }

 78         }

 79         else

 80         {

 81             if(dp2[y].value < dp1[x].value+edge[i].weight)

 82             {

 83                 dp2[y].value = dp1[x].value+edge[i].weight;

 84                 dp2[y].id = x;

 85                 if(dp1[y].value < dp2[y].value)

 86                     swap(dp1[y],dp2[y]);

 87             }

 88         }

 89         DFS2(y,x);

 90     }

 91 }

 92 

 93 int main()

 94 {

 95     int i,n;

 96     int x,y,k;

 97     while(scanf("%d",&n)!=EOF)

 98     {

 99         Init();

100         for(y=2; y<=n; ++y)

101         {

102             scanf("%d %d",&x,&k);

103             Add(x,y,k);

104             Add(y,x,k);

105         }

106         DFS1(1,-1);

107         DFS2(1,-1);

108         for(i=1; i<=n; ++i)

109             printf("%d\n",dp1[i].value);

110     }

111     return 0;

112 }

 

 


 

你可能感兴趣的:(com)