SGU 149. Computer Network( 树形dp )

题目大意:给N个点,求每个点的与其他点距离最大值

很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, dfs一遍得到. mx[x][2]表示从x的父亲到x的最长路径长度, 也是dfs一遍得到(具体看代码)。最后答案就是max(mx[x][0], mx[x][2]). 时间复杂度O(N)

--------------------------------------------------------------------------------------------

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

const int maxn = 10009;

int N, mx[maxn][3];

struct edge {

int to, w;

edge* next;

} E[maxn << 1], *pt = E, *head[maxn];

void AddEdge(int u, int v, int w) {

pt->to = v; pt->w = w; pt->next = head[u]; head[u] = pt++;

}

void Init() {

scanf("%d", &N);

for(int i = 1; i < N; i++) {

int t, w; scanf("%d%d", &t, &w); t--;

AddEdge(i, t, w);

AddEdge(t, i, w);

}

}

inline void upd(int x, int t) {

if(mx[x][1] < t)

mx[x][1] = t;

if(mx[x][0] < mx[x][1])

swap(mx[x][0], mx[x][1]);

}

void DFS0(int x, int fa = -1) {

mx[x][0] = mx[x][1] = 0;

for(edge* e = head[x]; e; e = e->next) if(e->to != fa) {

DFS0(e->to, x);

upd(x, mx[e->to][0] + e->w);

}

}

void DFS1(int x, int fa = - 1) {

for(edge* e = head[x]; e; e = e->next) if(e->to != fa) {

mx[e->to][2] = mx[x][2] + e->w;

if(mx[e->to][0] + e->w == mx[x][0])

mx[e->to][2] = max(mx[e->to][2], mx[x][1] + e->w);

else

mx[e->to][2] = max(mx[e->to][2], mx[x][0] + e->w);

DFS1(e->to, x);

}

}

int main() {

Init();

DFS0(0);

DFS1(mx[0][2] = 0);

for(int i = 0; i < N; i++)

printf("%d\n", max(mx[i][2], mx[i][0]));

return 0;

}

--------------------------------------------------------------------------------------------

149. Computer Network

time limit per test: 0.25 sec.
memory limit per test: 4096 KB

input: standard input
output: standard output

A school bought the first computer some time ago. 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 for each computer number Si - maximum distance, 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.

Input

There is natural number N (N<=10000) in the first line of input, 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

Write N lines in output file. i-th line must contain number Si for i-th computer (1<=i<=N).

Sample test(s)

Input



1 1 
1 2

Output




3

[submit]

[forum]


Author: Andrew V. Lazarev, Michael R. Mirzayanov
Resource: Saratov Subregional School Team Contest, 2002
Date: Fall, 2002

时间: 2024-10-11 17:00:57

SGU 149. Computer Network( 树形dp )的相关文章

SGU 149. Computer Network

时间限制:0.25s 空间限制:4M: 题意: 给出一颗n(n<=10000)个节点的树,和n-1条边的长度.求出这棵树每个节点到最远节点的距离: Solution: 对于一个节点,我们可以用DFS,在O(n)的时间内求出它的最远节点的距离. 显然对于10000个节点,不可能将每一个节点都这样求. 那么我们来看看,对于一个已经求过的节点我们可以做什么: 假设,有节点k,他有子节点p,两者距离为d 已经求得它的最远节点距离为dis1, 这时对他的子节点p来说,有两种情况: 一种是:p在k的与最远节

HDU 2196 Computer 经典树形DP

一开始看错题了,后来发现原来是在一颗带权的树上面求出距离每一个点的最长距离,做两次dfs就好,具体的看注释? #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #incl

HDU2196 Computer(树形DP)

和LightOJ1257一样,之前我用了树分治写了.其实原来这题是道经典的树形DP,感觉这个DP不简单.. dp[0][u]表示以u为根的子树中的结点与u的最远距离 dp[1][u]表示以u为根的子树中的结点与u的次远距离 这两个可以一遍dfs通过儿子结点转移得到.显然dp[0][u]就是u的一个可能的答案,即u往下走的最远距离,还缺一部分就是u往上走的最远距离: dp[2][u]表示u往上走的最远距离 对于这个的转移,分两种情况,是这样的: dp[2][v] = max( dp[0][u]+w

HDU 2136:Computer(树形DP)

http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 Computer 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 on

HDU 2196 Computer(树形DP求最长路)

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 a

题解报告:hdu 2196 Computer(树形dp)

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 a

HDU 2196——Computer(树形DP)

膜拜了NN个大神的代码,看了一整天,弱菜伤不起啊.求拜师啊 问题分析:求树上每个节点到其它节点的最远距离 每个节点到其它节点的最远距离就是以该节点为根的树所能达到的最大深度,这样子的话,要把每个节点转化为根,总共dfs的次数为节点数,肯定超时 于是~ 一个节点的最长路:1.从该节点往下取得最长路(子树部分)  2.从该节点往上取得的最长路(父节点往上的部分) 情况1:自下而上的dfs(先深搜后操作) 情况2:自上而下的dfs(先操作后深搜){ 如果节点u的子节点v在 以u为父亲的子树上的最长路,

HDU-2196 Computer (树形DP)

题目大意:在一棵带边权的有根树中,对于每个点,找出它与离它最远的那个点的之间的距离. 题目分析:对于一个点,离它最远的点只有两种情况,一是它到叶子节点的最远距离,一是与它父亲的距离加上他的父亲到叶子节点的最远距离.因为它的父亲到叶子的最远距离的那条路径可能恰好经过它自己,所以还要求出每个点到叶子节点的次大距离. 代码如下: # include<iostream> # include<cstdio> # include<vector> # include<cstri

树形DP求树的重心 --SGU 134

令一个点的属性值为:去除这个点以及与这个点相连的所有边后得到的连通分量的节点数的最大值. 则树的重心定义为:一个点,这个点的属性值在所有点中是最小的. SGU 134 即要找出所有的重心,并且找出重心的属性值. 考虑用树形DP. dp[u]表示割去u点,得到的连通分支的节点数的最大值. tot[u]记录以u为根的这棵子树的节点数总和(包括根). 则用一次dfs即可预处理出这两个数组.再枚举每个点,每个点的属性值其实为max(dp[u],n-tot[u]),因为有可能最大的连通分支在u的父亲及以上