Computer

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

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
 
 
Author
scnu
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1011 3456 1520 2242 1059 
题意:给n个点,1为根节点,给你的是棵树,然后找到各个节点离树上其他节点最远的距离;
思路:树形dp;
因为这是一颗树,除了根节点外其他每个节点都又且一个父亲节点,首先dfs更新每个节点到其子节点下端的最长距离和次长距离,然后最长距离还可能从父亲节点那更新。那么再dfs从父亲节点更新子节点的最长距离,这个时候,父亲节点的最长可能是通过当前要更新的子节点,那么就用次长路去更新。复杂度O(n);
  1 #include<iostream>
2 #include<string.h>
3 #include<algorithm>
4 #include<queue>
5 #include<math.h>
6 #include<stdlib.h>
7 #include<stack>
8 #include<stdio.h>
9 #include<ctype.h>
10 #include<map>
11 #include<vector>
12 #include<map>
13 using namespace std;
14 typedef struct acc
15 {
16 int id;
17 int val;
18 } ak;
19 vector<ak>vec[100005];
20 bool vis[100005];
21 typedef struct node
22 {
23 int id;
24 int mcost;
25 int scost;
26 int mid;
27 int sid;
28 node()
29 {
30 mcost = 0;
31 scost = 0;
32 mid = -1;
33 sid = -1;
34 }
35 } ss;
36 ss dp[100005];
37 int father[1000005];
38 void dfs1(int n);
39 void dfs2(int n);
40 int main(void)
41 {
42 int n,m;
43 while(scanf("%d",&n)!=EOF)
44 {
45 memset(father,-1,sizeof(father));
46 int i,j;
47 memset(dp,0,sizeof(dp));
48 for(i = 0; i < 100005; i++)
49 vec[i].clear();
50 for(i = 2; i <= n; i++)
51 {
52 int id,val;
53 scanf("%d %d",&id,&val);
54 ak a;
55 a.val = val;
56 a.id = i;
57 father[i] = id;
58 vec[id].push_back(a);
59 }
60 dfs1(1);
61 dfs2(1);
62 for(i = 1; i <= n; i++)
63 {
64 printf("%d\n",dp[i].mcost);
65 }
66 }
67 return 0;
68 }
69 void dfs1(int n)
70 {
71 for(int i = 0; i < vec[n].size(); i++)
72 {
73 ak d = vec[n][i];
74 {
75 dfs1(d.id);
76 if(dp[d.id].mcost+d.val > dp[n].mcost)
77 {
78 dp[n].scost = dp[n].mcost;
79 dp[n].sid = dp[n].mid;
80 dp[n].mcost = dp[d.id].mcost+d.val;
81 dp[n].mid = d.id;
82 }
83 else if(dp[d.id].mcost+d.val > dp[n].scost)
84 {
85 dp[n].scost = dp[d.id].mcost+d.val;
86 dp[n].sid = d.id;
87 }
88 }
89 }
90 }
91 void dfs2(int n)
92 {
93 for(int i = 0; i < vec[n].size(); i++)
94 {
95 ak d = vec[n][i];
96 if(dp[n].mid!=d.id)
97 {
98 if(dp[n].mcost + d.val > dp[d.id].mcost)
99 {
100 dp[d.id].scost = dp[d.id].mcost;
101 dp[d.id].sid = dp[d.id].mid;
102 dp[d.id].mcost = dp[n].mcost + d.val;
103 dp[d.id].mid = n;
104 }
105 else if(dp[n].mcost + d.val > dp[d.id].scost)
106 {
107 dp[d.id].scost = dp[n].mcost + d.val;
108 dp[d.id].sid = n;
109 }
110 }
111 else
112 {
113 if(dp[n].scost + d.val > dp[d.id].mcost)
114 {
115 dp[d.id].scost = dp[d.id].mcost;
116 dp[d.id].sid = dp[d.id].mid;
117 dp[d.id].mcost = dp[n].scost + d.val;
118 dp[d.id].mid = n;
119 }
120 else if(dp[n].scost + d.val > dp[d.id].scost)
121 {
122 dp[d.id].scost = dp[n].scost + d.val;
123 dp[d.id].sid = n;
124 }
125 }
126 dfs2(d.id);
127 }
128 }
 

最新文章

  1. WPF仿百度Echarts人口迁移图
  2. MatLab/HR
  3. 用javascript写Android和iOS naitve应用,实在炫酷。
  4. android布局属性详解
  5. C++数学、信号处理相关库
  6. TypeScript开篇:尝点新鲜和甜头
  7. Git技巧总结分享
  8. (1)建立一个名叫Cat的类: 属性:姓名、毛色、年龄 行为:显示姓名、喊叫 (2)编写主类: 创建一个对象猫,姓名为“妮妮”,毛色为“灰色”,年龄为2岁,在屏幕上输 出该对象的毛色和年龄,让该对象调用显示姓名和喊叫两个方法。
  9. Sikuli简介
  10. 工作中用到的linux命令
  11. 【转】Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask
  12. 触发器应用 trigger
  13. Preemption Context Switches 和 Synchronization Context Switches
  14. 【剑指offer】员工年龄排序
  15. Nginx 容器
  16. 5、Docker容器网络
  17. linux一些工具的安装(三)
  18. 纯MarkDown博客阅读体验优化
  19. http://www.cnblogs.com/chenmeng0818/p/6370819.html
  20. SQL-sqlHelper001

热门文章

  1. Oracle-distinct()用法、count(distinct( 字段A || 字段B))是什么意思?distinct多个字段
  2. List 去重的 6 种方法,这个方法最完美!
  3. BIO/NIO/AIO对比
  4. 零基础学习java------25--------jdbc
  5. netty系列之:手持framecodec神器,创建多路复用http2客户端
  6. 【分布式】ZooKeeper权限控制之ACL(Access Control List)访问控制列表
  7. 面向切面编程(Spring AOP)
  8. 【JavaWeb】【Eclipse】使用Eclipse创建我的第一个网页
  9. IT服务生命周期
  10. dart系列之:实时通讯,在浏览器中使用WebSockets