题目大意:给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. 正式生产环境下hadoop集群的DNS+NFS+ssh免password登陆配置

    博客地址:http://www.loveweir.com/ 环境虚拟机centos6.5 主机名h1  IP 192.168.137.11  作为DNS FNS的server 主机名h2  IP 19 ...

  2. 常用的sql server规范

    常见的字段类型选择1.字符类型建议采用varchar/nvarchar数据类型2.金额货币建议采用money数据类型3.科学计数建议采用numeric数据类型4. 自增长标识建议采用bigint数据类 ...

  3. bootstrap注意事项(四)表格

    1.基本实例 为任意 <table> 标签添加 .table 类可以为其赋予基本的样式 — 少量的内补(padding)和水平方向的分隔线.这种方式看起来很多余!?但是我们觉得,表格元素使 ...

  4. ASP.NET递归添加树节点

    表设计如图: id        title         parentid 1         asp.net   0 2         c#           0 3         c#_ ...

  5. input模糊搜索功能

    <!doctype html> <meta charset="utf-8"> <style type="text/css"> ...

  6. [转载自 文顶顶]iOS开发UI篇—程序启动原理和UIApplication

    一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序. (2)每一个应用都有自己的UIApplica ...

  7. iOS程序启动原理(简单)

    1.执行main -> 执行UIApplicationMain UIApplicationMain底层实现 1.创建UIApplication对象 2.创建UIApplication代理对象 3 ...

  8. 自适应Cell

        // //  ViewController.m //  04-自适应cell // //  Created by 

  9. 20151205--JDBC-2

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. EC读书笔记系列之9:条款16、17

    条款16 成对使用new和delete时要采取相同形式 记住: ★若你在new表达式中使用[ ],必须在相应的delete中也使用[ ],反之亦然 -------------------------- ...