题目大意:给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

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

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

  1. SGU 149 Computer Network 树DP/求每个节点最远端长度

    一个比较经典的题型,两次DFS求树上每个点的最远端距离. 参考这里:http://hi.baidu.com/oi_pkqs90/item/914e951c41e7d0ccbf904252 dp[i][ ...

  2. SGU 149. Computer Network

    时间限制:0.25s 空间限制:4M: 题意: 给出一颗n(n<=10000)个节点的树,和n-1条边的长度.求出这棵树每个节点到最远节点的距离: Solution: 对于一个节点,我们可以用D ...

  3. HDU2196 Computer(树形DP)

    和LightOJ1257一样,之前我用了树分治写了.其实原来这题是道经典的树形DP,感觉这个DP不简单.. dp[0][u]表示以u为根的子树中的结点与u的最远距离 dp[1][u]表示以u为根的子树 ...

  4. poj3417 Network 树形Dp+LCA

    题意:给定一棵n个节点的树,然后在给定m条边,去掉m条边中的一条和原树中的一条边,使得树至少分为两部分,问有多少种方案. 神题,一点也想不到做法, 首先要分析出加入一条边之后会形成环,形成环的话,如果 ...

  5. Uva LA 3902 - Network 树形DP 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  6. HDU 2136:Computer(树形DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 Computer     Description A school bought the fi ...

  7. hdu2196 Computer【树形DP】【换根法】

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

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

    Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du ...

  9. Computer (树形DP)

    A school bought the first computer some time ago(so this computer's id is 1). During the recent year ...

随机推荐

  1. S3C2440实现wifi、3G上网和迷你无线路由的制作(一)

    S3C2440实现wifi.3G上网和迷你无线路由的制作 fulinux 凌云实验室 本文将通过ARM.linux平台,借助RT2070/RT3070芯片的无线模块(或使用RT2070/RT3070芯 ...

  2. VS2010+Visual Assist X

    以前一直用VC++6.0,配一个VA,感觉也挺好用的.今天安装了VS2010,感觉还是有点不适应.然后安装了一个 Visual Assist X,主要是VS2010下破解VA有点小麻烦,中途也出现了一 ...

  3. C#中dynamic的正确用法【转】

    dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...

  4. ShareSDK的简化压缩和使用样例

    share sdk 太大了  于是自己 開始简化,  删除了 ipad 的图片和 framework ,  压缩了 sharesdk 中的 图片,用几k的图片 替换了几个 200多k 的图片 仅仅保留 ...

  5. Jackson 框架,轻易转换JSON【转】

    Jackson 框架,轻易转换JSON Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 前面有介绍过json-lib这个框架,在 ...

  6. Dell 2950服务器CPU-E1422错误解决方法

    .造成原因:CPU松动或者是硅胶损耗 .解决方法: .断掉电源,将其后盖打开(在手没有静电的情况下操作) .拔掉周围的排热扇 .按住关卡,将其CPU卸下:并使用清洁剂清理,再次给CPU上涂上硅胶(均匀 ...

  7. Web 应用配置Log4Net

    1.第一步:在web.config文件添加如下代码: [sourcecode language="csharp"] <configSections> <secti ...

  8. unity读取Sqlite数据库

    using UnityEngine; using System.Collections; using Mono.Data.Sqlite; using System.Data; public enum ...

  9. js计时器。

    <html> <head> <title> Nonove js clock 时钟 </title> <script type="text ...

  10. javascript自定义日期函数

    1.格式化日期(YYYY-MM-DD) 代码: var DateFormat = function (date) { if (!(date instanceof Date)) { date = dat ...