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的最长距离为其到子节点的最长距离和经过其跟节点的最长距离二者之中的最大者。
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 11111
using namespace std;
typedef long long int LL;
typedef struct{
int to, next, w;
}Node;
Node edge[MAX];
LL head[MAX], dp[MAX][];
void AddEdge(int u, int v, int w, int i){
edge[i].to = v;
edge[i].w = w;
edge[i].next = head[u];
head[u] = i;
}
void dfs_to_son(int i){
LL bigest = , biger = ;
for(int j = head[i];j != -;j = edge[j].next){
int v = edge[j].to;
dfs_to_son(v);
LL temp = dp[v][] + edge[j].w;
if(bigest <= temp){
biger = bigest;
bigest = temp;
}else if(temp > biger) biger = temp;
}
dp[i][] = bigest;
dp[i][] = biger;
}
void dfs_to_father(int i){
for(int j = head[i];j != -;j = edge[j].next){
int v = edge[j].to;
dp[v][] = max(dp[i][], dp[v][] + edge[j].w == dp[i][] ? dp[i][]:dp[i][]) + edge[j].w;
dfs_to_father(v);
}
}
int main(){
int n, u, w;
/* freopen("in.c", "r", stdin); */
while(~scanf("%d", &n)){
memset(dp, , sizeof(dp));
memset(head, -, sizeof(head));
for(int i = ;i <= n;i ++){
scanf("%d%d", &u, &w);
AddEdge(u, i, w, i-);
}
dfs_to_son();
dfs_to_father();
for(int i = ;i <= n;i ++) printf("%lld\n", max(dp[i][], dp[i][]));
}
return ;
}

HDOJ --- 2196 Computer的更多相关文章

  1. hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. HDOJ 2196 Computer 树的直径

    由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...

  3. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. hdu 2196 computer

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. hdu 2196 Computer 树形dp模板题

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. hdu 2196 Computer(树形DP)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. hdu 2196 Computer 树的直径

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

  8. HDU 2196 Computer (树dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...

  9. hdu 2196 Computer(树形DP经典)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. iOS开发——百度地图SDK集成

    (正在形成文档,待更新……)

  2. UIView的frame和bounds区别

    UIView的frame和bounds区别 iOS中,大家肯定对view和frame都不陌生,我们设置view在父view中的位置和大小时,只需要设置frame就可以了. 可能大家也有查过网上的一些资 ...

  3. bzoj2653:middle

    思路:首先容易想到二分答案,但如何去check呢,对于一段区间[l,r],把所有小于答案的都赋值为-1,大于等于它的都赋值为1,然后求左端点在[a,b],右端点在[c,d]的最大子串和即可(也就是区间 ...

  4. (转)怎么去掉Xcode工程中的某种类型的警告 Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int32

    问题描述  在我们的项目中,通常使用了大量的第三方代码,这些代码可能很复杂,我们不敢改动他们,可是作者已经停止更新了,当sdk升级或者是编译器升级后,这些遗留的代码可能会出现许许多多的警告,那么我们有 ...

  5. mysql 断电 启动不了 start: Job failed to start

    公司内部服务器,突然断电,造成无法启动的解决办法 把my.cnf中配置的datadir路径下的ib_logfile* (比如ib_logfile0, lb_logfile1....)文件移到另外一个目 ...

  6. 思维导图软件VYM

    http://www.insilmaril.de/vym/ 点击打开链接http://www.oschina.net/p/vym 有人说VYM就跟目录是一回事,确实是这样, 只不过与excel之类的比 ...

  7. c语言实现交换两个数的值

    C语言中要实现交换两个数的值,可以有很多种方法,具体如下所述. 不使用中间变量: // 异或, a^=b^=a^=b; a ^= b; b ^= a; a ^= b; // 加减 a = a + b; ...

  8. [CLR VIA C#] chapter2 building,packaging,deploying, and administering

    今天整理一下 assembly, 总感觉第一章 到 第三章 没怎么仔细看, 导致后面作者说前面说过的, 我就心里不舒服, 前面3章很干很涩, 好好啃啃, 先作一些简单笔记, 最后再来 整体整理, 反正 ...

  9. 前端工程的构建工具对比 Gulp vs Grunt

    1. Grunt -> Gulp 早些年提到构建工具,难免会让人联想到历史比较悠久的Make,Ant,以及后来为了更方便的构建结构类似的Java项目而出现的Maven.Node催生了一批自动化工 ...

  10. ECharts使用记

    系统开发厂商一直都使用基于Flash的图表解决方案,例如Fushioncharts.本人也曾略做研究,当时对js不熟,只能采用静态xml方式,颇为繁琐. 自从了解了html5的新特性,意识到基于Can ...