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 ...
随机推荐
- cordova安装中的坑
1.安装android环境直接略过! 2.安装node.js直接略过! 3.安装cordova npm install -g cordova npm uninstall cordova -g(这条是 ...
- LA 3177 Beijing Guards(二分法 贪心)
Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...
- OpenCV(7)-图像直方图
直方图定义可参考这里.图像的直方图用来表示图像像素的统计信息,它统计了图像每一个通道(如果是多通道)中,每个像素的个数(比例). 计算直方图 OpenCV提供了直接计算直方图的函数 void calc ...
- shell通过ftp实现上传/下载文件
直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...
- ps -aux
~]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.2 2900 852 ? Ss 11:49 ...
- 在windows下的mysql使用
具体可参照http://jingyan.baidu.com/article/3aed632e19b5e8701080918f.html 1.安装mysql. 直接百度搜索mysql下载.
- java中抽象类与接口的区别
1.abstract class 在 Java 语言中表示的是一种继承关系,一个类只能使用一次继承关系.但是,一个类却可以实现多个interface. 2.在abstract class 中可以有自己 ...
- 【C语言】中的布尔类型
C语言中的布尔类型 一.相关基础知识 首先bool true false为C++中的关键字,C语言中默认不支持这几个字符! 二.具体内容 在C89 (ANSI C)标准中没有定义与布尔类型相关的内 ...
- STM32下载方法
一.JLINK下载方法 1.硬件设置 Boot0,Boot1 = 0,*(测试通过) Boot0,Boot1 = 1,0或者0,1(未测试) 2.软件设置 MDK设置 ① 选择Project -> ...
- ADS的默认连接分析及编译器产生符号解惑
ADS的默认连接顺序是怎样的呢?例如下边从2440init.s中摘出的编译器符号又该怎样理解呢? BaseOfROM DCD |Image##RO##Base| TopOfROM ...