HDOJ --- 2196 Computer

Computer

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


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
 
思路:先选定1为树根,进行第一次深搜,很容易求出节点u到其子节点的最长距离和次长距离,求次长距离的目的是如果u的跟节点最长路径经过u则dp的时候就不能取其跟节点的最长距离,应该取其次长距离;然后进行第二次深搜,搜索节点u经过其跟节点的最长距离。
如果令dp[u][0],dp[u][1],分别为节点u到子节点的最长,次长距离,dp[u][2]表示节点u经过其跟节点v的最长距离,则状态方程为 dp[u][2] = max(dp[v][2],dp[u][0] + edge[j].w == dp[v][0] ? dp[v][1] : dp[v][0]) + edge[j].w;
最后输出答案为max(dp[u][0],dp[u][2]),即某点u的最长距离为其到子节点的最长距离和经过其跟节点的最长距离二者之中的最大者。
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define MAX 11111
 5 using namespace std;
 6 typedef long long int LL;
 7 typedef struct{
 8     int to, next, w;
 9 }Node;
10 Node edge[MAX];
11 LL head[MAX], dp[MAX][3];
12 void AddEdge(int u, int v, int w, int i){
13     edge[i].to = v;
14     edge[i].w = w;
15     edge[i].next = head[u];
16     head[u] = i;
17 }
18 void dfs_to_son(int i){
19     LL bigest = 0, biger = 0;
20     for(int j = head[i];j != -1;j = edge[j].next){
21         int v = edge[j].to;
22         dfs_to_son(v);
23         LL temp = dp[v][0] + edge[j].w;
24         if(bigest <= temp){
25             biger = bigest;
26             bigest = temp;
27         }else if(temp > biger) biger = temp;
28     }
29     dp[i][0] = bigest;
30     dp[i][1] = biger;
31 }
32 void dfs_to_father(int i){
33     for(int j = head[i];j != -1;j = edge[j].next){
34         int v = edge[j].to;
35         dp[v][2] = max(dp[i][2], dp[v][0] + edge[j].w == dp[i][0] ? dp[i][1]:dp[i][0]) + edge[j].w;
36         dfs_to_father(v);
37     }
38 }
39 int main(){
40     int n, u, w;
41     /* freopen("in.c", "r", stdin); */
42     while(~scanf("%d", &n)){
43         memset(dp, 0, sizeof(dp));
44         memset(head, -1, sizeof(head));
45         for(int i = 2;i <= n;i ++){
46             scanf("%d%d", &u, &w);
47             AddEdge(u, i, w, i-1);
48         }
49         dfs_to_son(1);
50         dfs_to_father(1);
51         for(int i = 1;i <= n;i ++) printf("%lld\n", max(dp[i][2], dp[i][0]));
52     }
53     return 0;
54 }

 

 

posted on 2014-04-03 21:45  ~Love()  阅读(127)  评论(0编辑  收藏  举报

导航