Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34390    Accepted Submission(s): 5383

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1561 1011 3456 1520 2242 

题意:

一棵有n个节点的树,有权边。问每个节点到其他节点的最远距离分别是多少。

思路:

如果固定一个节点,那么dfs找最远的距离就可以了。用dp[i]表示以i为根的子树中,i可到的最远距离。

对于任意一个节点i,他到达的最远节点有可能在子树中,也有可能不在子树中。所以我们分别找到子树中的最远距离和不在子树中的最远距离。

dfs1就是用dp存储了在子树中的最远距离。

dfs2就是把当前节点换到了根的位置,用f存储不在子树中的最远距离。那么他到他子树节点的距离是变短了的就不用管了。

把i换成根,把i的父亲father换下去,如果i在i的父亲这棵子树的最长路径上。father换下去之后,就要考虑原本以father为根的子树的第二长路径。

如果i不在father的最长路径上,那就是在f和dp中找大的加上当前路径长度。

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; const int maxn = ;
int head[maxn], dp[maxn], f[maxn], second[maxn], longest[maxn];
struct node{
int v;
int nxt;
int weight;
}edge[maxn * ];
int n, cnt; void addedge(int u, int v, int w)
{
edge[cnt].v = v;
edge[cnt].weight = w;
edge[cnt].nxt = head[u];
head[u] = cnt++;
edge[cnt].v = u;
edge[cnt].weight = w;
edge[cnt].nxt = head[v];
head[v] = cnt++;
} void dfs1(int rt, int fa)
{
for(int i = head[rt]; i != -; i = edge[i].nxt){
int son = edge[i].v;
if(son == fa)continue;
dfs1(son, rt);
//dp[rt] = max(dp[rt], dp[son] + edge[i].weight);
if(dp[son] + edge[i].weight > dp[rt]){
longest[rt] = son;
second[rt] = max(second[rt], dp[rt]);
dp[rt] = dp[son] + edge[i].weight;
}
else if(second[rt] < dp[son] + edge[i].weight){
second[rt] = dp[son] + edge[i].weight;
}
}
} void dfs2(int rt, int fa)
{
for(int i = head[rt]; i != -; i = edge[i].nxt){
int son = edge[i].v;
if(son == fa)continue;
if(longest[rt] == son)f[son] = max(f[rt], second[rt]) + edge[i].weight;
else f[son] = max(f[rt], dp[rt]) + edge[i].weight;
dfs2(son, rt);
}
} int main(){
while(scanf("%d", &n) != EOF){
cnt = ;
memset(head, -, sizeof(head));
for(int i = ; i <= n; i++){
int u, w;
scanf("%d%d", &u, &w);
addedge(u, i, w);
}
memset(dp, , sizeof(dp));
memset(second, , sizeof(second));
memset(longest, , sizeof(longest));
memset(f, , sizeof(f));
dfs1(, ); dfs2(, );
//f[1] = dp[1];
for(int i = ; i <= n; i++){
printf("%d\n", max(dp[i], f[i]));
}
}
return ;
}

hdu2196 Computer【树形DP】【换根法】的更多相关文章

  1. bzoj 3743 [Coci2015]Kamp——树形dp+换根

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3743 树形dp+换根. “从根出发又回到根” 减去 “mx ” . 注意dfsx里真的要改那 ...

  2. poj3585 Accumulation Degree[树形DP换根]

    思路其实非常简单,借用一下最大流求法即可...默认以1为根时,$f[x]$表示以$x$为根的子树最大流.转移的话分两种情况,一种由叶子转移,一种由正常孩子转移,判断一下即可.换根的时候由頂向下递推转移 ...

  3. 树形dp换根,求切断任意边形成的两个子树的直径——hdu6686

    换根dp就是先任取一点为根,预处理出一些信息,然后在第二次dfs过程中进行状态的转移处理 本题难点在于任意割断一条边,求出剩下两棵子树的直径: 设割断的边为(u,v),设down[v]为以v为根的子树 ...

  4. poj3585 Accumulation Degree(树形dp,换根)

    题意: 给你一棵n个顶点的树,有n-1条边,每一条边有一个容量z,表示x点到y点最多能通过z容量的水. 你可以任意选择一个点,然后从这个点倒水,然后水会经过一些边流到叶节点从而流出.问你最多你能倒多少 ...

  5. [题解](树形dp/换根)小x游世界树

    2. 小x游世界树 (yggdrasi.pas/c/cpp) [问题描述] 小x得到了一个(不可靠的)小道消息,传说中的神岛阿瓦隆在格陵兰海的某处,据说那里埋藏着亚瑟王的宝藏,这引起了小x的好奇,但当 ...

  6. HDU2196 - Computer(树形DP)

    题目大意 给定一颗n个结点的树,编号为1~n,要求你求出每个结点能到达的最长路径 题解 用动态规划解决的~~~~把1 当成树根,这样就转换成有根树了.我们可以发现,对于每个结点的最长路,要么是从子树得 ...

  7. cf219d 基础换根法

    /*树形dp换根法*/ #include<bits/stdc++.h> using namespace std; #define maxn 200005 ]; int root,n,s,t ...

  8. 题解 poj3585 Accumulation Degree (树形dp)(二次扫描和换根法)

    写一篇题解,以纪念调了一个小时的经历(就是因为边的数组没有乘2 phhhh QAQ) 题目 题目大意:找一个点使得从这个点出发作为源点,流出的流量最大,输出这个最大的流量. 以这道题来介绍二次扫描和换 ...

  9. poj 3585 Accumulation Degree(二次扫描和换根法)

    Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总 ...

  10. poj3585树最大流——换根法

    题目:http://poj.org/problem?id=3585 二次扫描与换根法,一次dfs求出以某个节点为根的相关值,再dfs遍历一遍树,根据之前的值换根取最大值为答案. 代码如下: #incl ...

随机推荐

  1. 【转】【Linux】Linux 命令行快捷键

    Linux 命令行快捷键 涉及在linux命令行下进行快速移动光标.命令编辑.编辑后执行历史命令.Bang(!)命令.控制命令等.让basher更有效率. 常用 ctrl+左右键:在单词之间跳转 ct ...

  2. (38)JS运动之淡入淡出

    基本思路:使用样式filter.可是要区分IE浏览器和chrom.firefox的不同,详细也是用定时器来实现. <!DOCTYPE HTML> <!-- --> <ht ...

  3. NSArray打印汉字的方法

    (1) NSArray打印汉字 通过重载NSArray的- (NSString *)descriptionWithLocale:(id)locale方法 方法体例如以下: //依据设置的locale ...

  4. rsync文件同步、Inotify-tools参数详解

    inotifywait用于等待文件或文件集上的一个待定事件,可以监控任何文件和目录设置,并且可以递归地监控整个目录树: inotifywatch用于收集被监控的文件系统计数据,包括每个inotify事 ...

  5. 详解MathType引用公式编号功能

    在论文创作期间,如果需要在文本中删除大量的公式,手动编号删除的工作量是比较大的,使用MathType引用公式编号功能就可以节约大量的时间,提供很大的方便.本教程将详解MathType引用公式编号功能. ...

  6. 如何创建 SVN 服务器,并搭建自己的 SVN 仓库

    SVN是一个广泛使用的版本控制系统,在日常工作中有广泛的应用空间,比如文档管理,我们就可以搭建一个 SVN 仓库,日常需要归类保存的文档统统上传到仓库中,那如何创建 SVN 服务器, 又如何搭建 SV ...

  7. Linux内核中断处理体系分析

    前一篇博文中:linux内核初始化阶段通过early_trap_init()函数完毕了把异常向量复制到0xFFFF0000開始的地方,这些异常向量大部分指向通过vector_stub宏定义的那段代码. ...

  8. list的下标【python】

    转自:http://www.cnblogs.com/dyllove98/archive/2013/07/20/3202785.html list的下表从零开始,和C语言挺类似的,但是增加了负下标的使用 ...

  9. oracle12c创建用户和表空间出现的问题

    Oracle12c 中,增加了可插接数据库的概念,即PDB,允许一个数据库容器(CDB)承载多个可插拔数据库(PDB).CDB全称为 ContainerDatabase,中文翻译为数据库容器,PDB全 ...

  10. PHP虚拟主机的配置

    今天配置了一下虚拟目录,以下是我的配置方法. 1  编辑httpd.conf,查找Include conf/extra/httpd-vhosts.conf,把前面注释符号“#”删掉. 2  编辑htt ...