博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU2196(SummerTrainingDay13-D tree dp)
阅读量:4305 次
发布时间:2019-06-06

本文共 3543 字,大约阅读时间需要 11 分钟。

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 30923    Accepted Submission(s): 3861

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
 
题意:问从每个几点出发所到达的最远距离。
思路:两遍dfs,一遍从上往下,一遍从下往上,答案为往上走或往下走的最大值。
1 //2017-09-13 2 #include 
3 #include
4 #include
5 #include
6 7 using namespace std; 8 9 const int N = 11000;10 11 int head[N], tot;12 struct Edge{13 int v, w, next;14 }edge[N<<1];15 16 void init(){17 tot = 0;18 memset(head, -1, sizeof(head));19 }20 21 void add_edge(int u, int v, int w){22 edge[tot].v = v;23 edge[tot].w = w;24 edge[tot].next = head[u];25 head[u] = tot++;26 }27 28 //down[u][0]表示u节点往下走的最大距离,down[u][1]表示节点u往下走的次大距离29 //up[u]表示节点u往上走的最大距离,son[u]表示u节点往下走的最大距离对应的儿子30 int n, down[N][2], up[N], son[N];31 32 void dfs1(int u, int fa){33 for(int i = head[u]; i != -1; i = edge[i].next){34 int v = edge[i].v, w = edge[i].w;35 if(v == fa)continue;36 dfs1(v, u);37 if(down[v][0]+w > down[u][0]){
//更新最大的情况38 down[u][1] = down[u][0];39 down[u][0] = down[v][0]+w;40 son[u] = v;41 }else if(down[v][0]+w > down[u][1])//只更新次大值的情况42 down[u][1] = down[v][0] + w;43 }44 }45 46 void dfs2(int u, int fa){47 for(int i = head[u]; i != -1; i = edge[i].next){48 int v = edge[i].v, w = edge[i].w;49 if(v == fa)continue;50 if(son[u] != v)51 up[v] = max(up[u]+w, down[u][0]+w);52 else53 up[v] = max(up[u]+w, down[u][1]+w);54 dfs2(v, u);55 }56 }57 58 int main()59 {60 //freopen("inputD.txt", "r", stdin);61 while(scanf("%d", &n) != EOF){62 init();63 int v, w;64 for(int i = 2; i <= n; i++){65 scanf("%d%d", &v, &w);66 add_edge(i, v, w);67 add_edge(v, i, w);68 }69 memset(up, 0, sizeof(up));70 memset(down, 0, sizeof(down));71 dfs1(1, 0);72 dfs2(1, 0);73 for(int i = 1; i <= n; i++)74 printf("%d\n", max(up[i], down[i][0]));75 }76 77 return 0;78 }

 

转载于:https://www.cnblogs.com/Penn000/p/7514929.html

你可能感兴趣的文章
ansible 判断和循环
查看>>
小W计树
查看>>
OpenSSL漏洞补救办法详解(转)
查看>>
opencv 和 parfor
查看>>
Raspberry Pi For Windows
查看>>
改错3-38
查看>>
开发 调试
查看>>
centos 系统管理维护指南
查看>>
使用wireshark抓包工具 检测不到本地网卡
查看>>
C# 网络编程之webBrowser获取网页url和下载网页中图片
查看>>
HDU 3664 Permutation Counting(DP)
查看>>
Tomcat 配置文件
查看>>
es6-变量的解构赋值
查看>>
百度知道回答的依赖注入
查看>>
ubuntu16.04 编译出错:fatal error: SDL/SDL.h: No such file or directory
查看>>
错题集03
查看>>
Unity游戏开发之C#快速入门
查看>>
Jenkins插件之Deploy
查看>>
二分图匹配之匈牙利算法
查看>>
Josn
查看>>