HDU

先上题目:

Computer

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


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. 


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
 
  题意:给你一棵树,边上有权值求每个节点可以到达的最远距离是多少。
  粗看这题像是求最长路,然后一看节点有10000个就要考虑是否真的要用dij或者spfa了,因为这是一棵树,只有n-1条边。这里我是用的是树DP的方法→_→。
  思路,对于某个节点,它能到达的最长距离可能在两个方向上:在它的子树里,或者在它的父节点的方向上。所以这里我们要做两次dfs。
  第一次dfs,求出以某个节点i为根的子树,i其子树上一条分支上可走的最远距离和次远距离。
  第二次dfs,求出从父节点方向到这个节点i的最远距离。这里就可以解释为什么要在第一次dfs求次远距离了。因为父节点可以到达的最远距离可能来自当前正在分析的这一条分支,这是从父节点方向到节点i的最远距离就等于父节点到i的距离加上父节点可以到达的次短距离了。
  那么对于某一个节点i可以到达的最远距离就是max{第一次dfs该点的结果,第二次dfs该点的结果},(最远距离只会来自这两个方向)
  这一题第一次做wa了一次因为第二次dfs的时候修改了根节点的最长距离来向,实际上这一题中第二次dfs用到了第一次dfs的结果,第二次dfs的结果并不是最终结果,还需要和第一次dfs的结果作比较才能得到最终结果。
 
上代码:
 
 1 #include <cstdio>
 2 #include <cstring>
 3 #define max(x,y) (x > y ? x : y)
 4 #define max_(x,y,z) ( max(x,y) , z )
 5 #define MAX 10001
 6 using namespace std;
 7 
 8 int tdis[MAX][2];
 9 int p[MAX];
10 int tot;
11 typedef struct{
12     int c;
13     int l;
14     int to;
15 }tree;
16 tree t[MAX];
17 int path[MAX];
18 int dis[MAX];
19 
20 void add(int u,int v,int l){
21     t[tot].c=v;
22     t[tot].l=l;
23     t[tot].to=p[u];
24     p[u]=tot++;
25 }
26 
27 void dfs1(int r){
28     int len;
29     for(int i=p[r];i!=-1;i=t[i].to){
30         dfs1(t[i].c);
31         len=tdis[t[i].c][0]+t[i].l;
32         if(len>=tdis[r][0]){
33             tdis[r][1]=tdis[r][0];
34             tdis[r][0]=len;
35             path[r]=i;
36         }else if(len>tdis[r][1]){
37             tdis[r][1]=len;
38         }
39     }
40 }
41 
42 void dfs2(int r){
43     int len;
44     for(int i=p[r];i!=-1;i=t[i].to){
45         if(path[r]==i){
46             len=max(dis[r],tdis[r][1])+t[i].l;
47         }else{
48             len=max(dis[r],tdis[r][0])+t[i].l;
49         }
50         dis[t[i].c]=len;
51         dfs2(t[i].c);
52     }
53 }
54 
55 int main()
56 {
57     int n,a,l;
58     //freopen("data.txt","r",stdin);
59     while(scanf("%d",&n)!=EOF){
60         tot=0;
61         memset(p,-1,sizeof(p));
62         memset(t,0,sizeof(t));
63         for(int i=2;i<=n;i++){
64             scanf("%d %d",&a,&l);
65             add(a,i,l);
66         }
67         memset(tdis,0,sizeof(tdis));
68         memset(dis,0,sizeof(dis));
69         memset(path,0,sizeof(path));
70         dfs1(1);
71         dis[1]=0;
72         dfs2(1);
73         for(int i=1;i<=n;i++){
74             printf("%d
",max(dis[i],tdis[i][0]));
75         }
76         //printf("
");
77     }
78     return 0;
79 }
2196
原文地址:https://www.cnblogs.com/sineatos/p/3555958.html