SGU 149. Computer Network
时间限制: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的更多相关文章
- SGU 149. Computer Network( 树形dp )
题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...
- SGU 149 Computer Network 树DP/求每个节点最远端长度
一个比较经典的题型,两次DFS求树上每个点的最远端距离. 参考这里:http://hi.baidu.com/oi_pkqs90/item/914e951c41e7d0ccbf904252 dp[i][ ...
- Sgu149 Computer Network
Sgu149 Computer Network 题目描述 给你一棵N(N<=10000)个节点的树,求每个点到其他点的最大距离. 不难想到一个节点到其他点的最大距离为:max(以它为根的子树的最 ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- (中等) CF 555E Case of Computer Network,双连通+树。
Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...
- [J]computer network tarjan边双联通分量+树的直径
https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...
- Computer Network Homework2’s hard question
Computer Network Homework2’s hard question 2. What is the signal which is used to modulate the origi ...
- Computer Network Homework3’ s hard question
Computer Network Homework3’ s hard question 1. Which kind of protocol does CSMA belong to? A. Random ...
随机推荐
- Delphi调试CGI或ISAPI 转
因为dll文件已驻留内存,可用intrabob进行调试,也可用PWS进行调试,不过要换文件. IntraBob是资深程序员Dr.Bob编写的免费工具软件,用于测试Delphi编写 的CGI/Win ...
- Delphi WebService 需要注意 转
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://gang4415.blog.51cto.com/225775/251997 Web ...
- 数据库的存储引擎和SQL语言
数据库的存储引擎就是管理数据存储的东西,它完成下面的工作: 1)存储机制 2)索引方式 3)锁 4)等等 SQL语言:-----关系型数据库所使用的数据管理语言 1)数据定义语言(DDL):DROP. ...
- 机器安装第二个tomcat ,出现报错如何解决
1.本机安装第二个 tomcat 后,出现 报错如下图所示 最后解决办法 是 在安装的时候 ,windows 服务名称 和 另一个tomcat 起不一样的 名称就可以了 如下图
- Git error: hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused b
hint: Updates were rejected because the remote contains work that you dohint: not have locally. This ...
- CSS 属性 - 伪类和伪元素
CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪元素用于将特殊的效果添加到某些选择器. 可以明确两点,第一两者都与选择器相关,第二就是添加一些“特殊”的效果.这里特殊指的是两者描述了其他 cs ...
- 两种常用的启动和关闭MySQL服务
本博文的主要内容有 .通过图形界面来启动和关闭MySQL服务 .通过DOS窗口来启动和关闭MySQL服务 1.通过图形界面来启动和关闭MySQL服务 2.通过DOS窗口来启动和关闭MySQL服务 感谢 ...
- 外显子分析:cutadapt,去除序列adapter详细解析
外显子测序时带有adapt接头,因此我们需要去除adapt接头,cutadapt的作用是去除adapt接头,一般用到如下命令: cutadapt -a AACCGGTT -o output.fastq ...
- 用java写随机出题
import java.io.*; //输入函数包 public class hello{ public static void main(String args[]){ String s=" ...
- lua字符匹配
匹配下列格式的数据中的 source和MAC地址: Chain WiFiDog_br-lan_Outgoing (1 references) pkts bytes target prot opt in ...