时间限制:0.25s

空间限制:4M;

题意:

给出一颗n(n<=10000)个节点的树,和n-1条边的长度。求出这棵树每个节点到最远节点的距离;


Solution:

对于一个节点,我们可以用DFS,在O(n)的时间内求出它的最远节点的距离.

显然对于10000个节点,不可能将每一个节点都这样求.

那么我们来看看,对于一个已经求过的节点我们可以做什么:

假设,有节点k,他有子节点p,两者距离为d

已经求得它的最远节点距离为dis1,

这时对他的子节点p来说,有两种情况:

一种是:p在k的与最远节点的路径上.

这时p的最远距离等于max(dis1-d,k的次远距离+d);

另一种是:p不在k的最远路径上.

此时p的最远距离等于max(dis1+d,p向下的最远距离);

通过上面我们发现,我们需要一个节点的最远距离和次远距离以及p向下的最远距离.

幸运的是这三个量都可以通过一次对根的DFS在O(n)的时间内求出.

最后再从根进行一次DFS遍求出每个节点的最远距离和次远距离就可以求出所有的答案了.

总的时间复杂度O(n),空间复杂度O(n);

code

#include <iostream>
#include <cstdio>
#include <vector>
#include <utility>
using namespace std; #define mp make_pair
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define rd(a) scanf("%d",&a)
#define rdd(a,b) scanf("%d%d",&a,&b);
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = 11111; vii edge[INF];
int dis[INF][2], ans[INF];
int n, x, y;
int dfs (int x) {
dis[x][0] = 0;
rep (i, 0, sz(edge[x]) - 1) {
ii v = edge[x][i];
int tem = dfs (v.fi)+v.se;
rep (i, 0, 1) if (tem > dis[x][i]) swap (tem, dis[x][i]);
}
return dis[x][0];
}
void DP (int x) {
int tem;
ans[x] = dis[x][0];
rep (i, 0, sz (edge[x]) - 1) {
ii v = edge[x][i];
if (dis[v.fi][0] + v.se == dis[x][0])
tem = dis[x][1] + v.se;
else
tem = dis[x][0] + v.se;
rep (i, 0, 1) if (tem > dis[v.fi][i]) swap (tem, dis[v.fi][i]);
DP (v.fi);
}
}
int main() {
rd (n);
rep (i, 2, n) {
rdd (x, y);
edge[x].pb (mp (i, y) );
}
dfs (1);
DP (1);
rep (i, 1, n) printf ("%d\n", ans[i]);
}

  

SGU 149. Computer Network的更多相关文章

  1. SGU 149. Computer Network( 树形dp )

    题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...

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

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

  3. Sgu149 Computer Network

    Sgu149 Computer Network 题目描述 给你一棵N(N<=10000)个节点的树,求每个点到其他点的最大距离. 不难想到一个节点到其他点的最大距离为:max(以它为根的子树的最 ...

  4. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  5. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  6. (中等) CF 555E Case of Computer Network,双连通+树。

    Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...

  7. [J]computer network tarjan边双联通分量+树的直径

    https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...

  8. Computer Network Homework2’s hard question

    Computer Network Homework2’s hard question 2. What is the signal which is used to modulate the origi ...

  9. Computer Network Homework3’ s hard question

    Computer Network Homework3’ s hard question 1. Which kind of protocol does CSMA belong to? A. Random ...

随机推荐

  1. Linux Shell编程(29)——函数

    和"真正的"编程语言一样, Bash也有函数,虽然在某些实现方面稍有些限制. 一个函数是一个子程序,用于实现一串操作的代码块,它是完成特定任务的"黑盒子". 当 ...

  2. redis3.0集群使用发现的一些问题

    1.看了官方文档,没有发现有关整个集群关闭再启动的方法.集群是多机器多节点运行,一般情况不可能出现所有机器都挂掉.但万一同时挂掉,数据丢失的可能性就极大了. 验证方法:手动关闭了集群中所有节点,然后再 ...

  3. java jdbc使用配置文件连接数据库:

    java jdbc使用配置文件连接数据库: 创建后缀名为:.properties的文件,文件内容包括,数据库驱动.连接的数据库地址.用户名.密码…… 以Mysql为例创建config.properti ...

  4. Linux学习笔记3——Linux中常用系统管理命令

    stat 显示指定文件的相关信息,比ls命令显示内容更多 who 显示在线登录用户 hostname 显示主机名称 uname显示系统信息 top 显示当前系统中耗费资源最多的进程 ps 显示瞬间的进 ...

  5. ClientScriptManager与ScriptManager向客户端注册脚本的区别

    使用ClientScriptManager向客户端注册脚本 ClientScriptManager在非异步(就是说非AJAX)环境下使用的.如果要在异步环境下注册脚本应该使用ScriptManager ...

  6. 在word 2013中输入latex公式

    注意:版权所有,转载请注明出处 向word输入LaTeX公式,插件有很多,前面在使用的是一个叫做Aurora的插件,结果不是免费的,用了一段时间就要收费是,所以就不用了,从网上找到别人的介绍,可以使用 ...

  7. hive UDAF源代码分析

    sss /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license a ...

  8. HTTPClient实现java自动登录人人网

    参考网址: https://passport.csdn.net/account/login  http://www.iteye.com/topic/638206 httpClient http://b ...

  9. Python基础知识--列表和集合

    列表:有序性,可以存放任意类型的对象,通过索引访问,可以分片操作 >>> L = ['id', 1000, 'scd', 1000, 'scd'] >>> L [' ...

  10. The Boost C++ Libraries

    " ...one of the most highly regarded and expertly designed C++ library projects in the world.&q ...