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
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.
#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的更多相关文章
- hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDOJ 2196 Computer 树的直径
由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2196 computer
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2196 Computer 树形dp模板题
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 2196 Computer(树形DP)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2196 Computer 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- hdu 2196 Computer(树形DP经典)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- iOS开发-自动布局篇:史上最牛的自动布局教学!
转载自:http://www.jianshu.com/p/f6cf9ef451d9 本文我们将提到: aotulayout(手码) VFL aotulayout(Xib) Masonry(第三方框架) ...
- IOS GCD 的理解
GCD (Grand Central Dispatch) 是Apple公司开发的一种技术,它旨在优化多核环境中的并发操作并取代传统多线程的编程模式. 在Mac OS X 10.6和IOS 4.0之后开 ...
- 无责任共享 Coursera、Udacity 等课程视频
本文转载自网络,原作者不详. (本文是用 markdown 写的,访问 https://www.zybuluo.com/illuz/note/71868 获得更佳体验) 程序语言 interactiv ...
- 处理safari缓存的办法
window.onpageshow = function(event) { if (event.persisted) { alert("From bac ...
- Makefile与shell脚本的区别
引用博客:Makefile与shell脚本区别 在Makefile可以调用shell脚本,但是Makefile和shell脚本是不同的.本文试着归纳一下Makefile和shell脚本的不同. 1.s ...
- POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数
题目链接: http://poj.org/problem?id=2299 题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数. 利用归并排序求解,内存和耗时都比较少, 但是有 ...
- Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)
Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...
- Discuz帖子列表页无法ajax加载下一页问题
上周末抽空重构了一下JX3PVE的PVE栏目,只上线了宏库栏目,结果出了一堆Bug.奈何公司这段时间都在搞完美假期这专题,太忙也没去处理.这不是周末拿时间来看一下. 发现其中有一个是点“下一页”aja ...
- bzoj 3572: [Hnoi2014]世界树 虚树 && AC500
3572: [Hnoi2014]世界树 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 520 Solved: 300[Submit][Status] ...
- jquery mobile backbone
http://www.appliness.com/getting-started-with-html-mobile-application-development-using-jquery-mobil ...