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. “Chaos”的算法之Floyd算法

    倘若我们要在计算机上建立一个交通咨询系统则可以采用图的结构来表示实际的交通网络.其实现最基本的功能,求出任意两点间的最短路径, 求最短路径的经典方法有很多种,最常用的便是迪杰斯特拉算法和佛洛依德(Fl ...

  2. 3D游戏与计算机图形学中的数学方法-视截体

    视截体用来表示一个空间的范围,位于这个空间范围内的三维场景的任何物体都可以被看到. 视截体由六个平面围成,其中的四个平面与场景的边界相对应,分别被称为左,右,底,顶视截面.另外两个平面称为近视截面和远 ...

  3. javascript 哈夫曼树构造

    function Node(data) { this.data = data; this.left = null; this.right = null; } Array.prototype.creat ...

  4. spring如何引用properties文件里的配置

      1.PropertyPlaceholderConfigurer类它是把属性中的定义的变量(var)替代,spring的配置文件中使用${var}的占位符 <beans><bean ...

  5. [转]ASP.NET MVC 5– 使用Wijmo MVC 5模板1分钟创建应用

    开始使用 使用ComponentOne Studio for ASP.NET Wijmo制作MVC5应用程序,首先要做的是安装Studio for ASP.NET Wijmo . 测试环境 VS201 ...

  6. 浅谈无缓存I/O操作和标准I/O文件操作差别

    首先,先略微了解系统调用的概念:        系统调用,英文名system call,每一个操作系统都在内核里有一些内建的函数库,这些函数能够用来完毕一些系统系统调用把应用程序的请求传给内核,调用对 ...

  7. CUGBACM Codeforces Tranning 1 题解

    链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=61581#overview 描写叙述:非常老的CF题,题不错,拿来训练正好. 做的时 ...

  8. Java精选笔记_国际化

    国际化 什么是国际化 指软件在开发时就应该具备支持多种语言和地区的功能,当应对不同国家和地区的用户访问,针对不同国家和地区的用户,提供相应的.符合来访者阅读习惯的页面和数据. 由于国际化interna ...

  9. day7_直播_网络编程篇(元昊老师著)

    网络编程篇计算机网络: 多台独立的计算机用网络通信设备连接起来的网络.实现资源共享和数据传递. 比如,我们之前的学过的知识可以将D盘的一个文件传到C盘,但如果你想从你的电脑传一个文件到我的电脑上目前是 ...

  10. RF使用的相关库API

    RF内置库: http://robotframework.org/robotframework/ SSHLibrary:   ---WEB自动化测试 http://robotframework.org ...