题目大意:给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. C#下使用GDAL

    參考博客:http://blog.csdn.net/rrrrssss00/article/category/915498.以及viewmode=contents">李民录老师专栏.ht ...

  2. 【Apache ZooKeeper】为ZNode设置watcher

    众所周知,ZooKeeper中的ZNode是树形结构,现在我需要给/app1结点设置watcher,监听/app1下增减.删除和修改的结点,并将相应的事件使用log4j记录到日志文件中.ZNode的变 ...

  3. MAC xampp 启动失败

    原文地址: http://meiyitianabc.blog.163.com/blog/static/1050221272013116232752/ 问题:80port被暂用,导致server无法启动 ...

  4. 怎样解决xcode里开发cocos2dx改动lua脚本后不刷新的问题

    用xcode来开发cocos2dx,结果发现一个非常纠结的问题,假设我一旦改动了一个Lua文件,我必须clean之后再build,否则改动的Lua文件不会体现出来.这是一个非常令纠结的结果,特别是我要 ...

  5. AssetBundle的使用

    using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class Editor ...

  6. 基于php常用正则表达整理(下)

    61        \n 匹配一个换行符.等价于 \x0a 和 \cJ.62        \r 匹配一个回车符.等价于 \x0d 和 \cM.63        \s 匹配任何空白字符,包括空格.制 ...

  7. Java SE基础部分——常用类库之SimpleDateFormat(日期格式化)

    取得当前日期,并按照不同日期格式化输入.代码如下: // 20160618 SimpleDateFomat类的使用 日期格式化 练习 package MyPackage; //自己定义的包 impor ...

  8. 【codevs】2292图灵机游戏

    题 Shadow最近知道了图灵机是什么(Shadow:就是一行格子和一个机器头移来移去的呗!),于是他突发奇想,创造了一个新游戏——“图灵机游戏”(Shadow:好听吧?). 游戏规则如下: 在一条长 ...

  9. 加密传输SSL协议7_SSL协议概述

    SSL(Secure Sockets Layer) SSL的功能,可以在通信的双方中建立一个加密的通信通道 同时还可以确认通信的双方是不是就是其声称的人,防止被钓鱼. SSL在网络协议栈中的位置:可以 ...

  10. op cache config

    [opcache] ; dll地址 zend_extension=php_opcache.dll ; 开关打开 opcache.enable=1 ; 开启CLI opcache.enable_cli= ...