博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 2196 Computer【树形DP】
阅读量:7023 次
发布时间:2019-06-28

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

 

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
分析:  两次DFS 第一次 找出每个节点的最远和次远距离  自下往上
               第二次 找出每个节点不包含该节点下方节点的最远距离。
View Code
#include
#include
int max(int a,int b){ return a>b?a:b;}struct node{ int to,next,w;}q[1000000];int tot;int head[10005];int f[10005]; // 当前节点往下的最远距离int s[10005]; // 当前节点往下的次远距离int d[10005]; // 当前节点不包括该节点下方的最远距离int l[10005]; // 当前节点最远路径上的直接儿子void add(int v,int u,int wi){ q[tot].to=u; q[tot].next=head[v]; q[tot].w=wi; head[v]=tot++;}void dfs(int r){ int i; if(f[r])return; int son,flag=1; int tmp=-1,xi=-1; for(i=head[r];i;i=q[i].next) { flag=0; son=q[i].to; dfs(son); if(f[son]+q[i].w>tmp) { tmp=f[son]+q[i].w; xi=son; } } if(flag) { f[r]=0; s[r]=0; return; } f[r]=tmp; l[r]=xi; tmp=-1; int x2=-1; for(i=head[r];i;i=q[i].next) { son=q[i].to; if(son!=xi&&f[son]+q[i].w>tmp) { tmp=f[son]+q[i].w; x2=son; } } if(x2!=-1) s[r]=tmp;}void dis(int r){ int i,son; for(i=head[r];i;i=q[i].next) { son=q[i].to; if(son==l[r]) d[son]=max(d[r],s[r])+q[i].w; else d[son]=max(d[r],f[r])+q[i].w; dis(son); } }int main(){ int n,i,a,b; while(scanf("%d",&n)!=EOF) { tot=1; memset(head,0,sizeof(head)); memset(f,0,sizeof(f)); memset(s,0,sizeof(s)); memset(d,0,sizeof(d)); memset(l,-1,sizeof(l)); for(i=2;i<=n;i++) { scanf("%d%d",&a,&b); add(a,i,b); } dfs(1); d[1]=0; dis(1); for(i=1;i<=n;i++) { int res=max(s[i],d[i]); res=max(res,f[i]); printf("%d\n",res); } } return 0;}

转载于:https://www.cnblogs.com/dream-wind/archive/2012/04/18/2456056.html

你可能感兴趣的文章
阿里有群姑娘,是马老师的师兄,还是逍遥子的学长……
查看>>
Docker 在 Coding WebIDE 项目中的运用
查看>>
ATtiny85 制作迷你小游戏机
查看>>
jQuery限制“Text-Area”域中的字符的个数
查看>>
bboss框架配置监控介绍
查看>>
Fastify 2.0.1 和 1.14.3 发布,极速 Node.js Web 框架
查看>>
和平之翼代码生成器SMEU版 4.0.0 Beta5 宝船公布
查看>>
Android--进程间通信(Binder)
查看>>
Spring Boot 实现json和jsonp格式数据接口
查看>>
八大排序的Java实现
查看>>
练字之《短歌行》
查看>>
Spring+quartz cron表达式(cron手册官方)完美理解
查看>>
性能分析系列-小命令保证大性能
查看>>
BottledWater-PG:PostgreSQL集成Kafka的实时数据交换平台
查看>>
Java 学习(03)--运算符/循环小结
查看>>
scala :冒泡排序
查看>>
PostgreSQL 11 preview - 通用场景性能 增强 汇总
查看>>
Fatal error in launcher:Unable to create process using
查看>>
我用 tensorflow 实现的“一个神经聊天模型”:一个基于深度学习的聊天机器人...
查看>>
1015. Reversible Primes (20)
查看>>