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 ...
随机推荐
- Linux Shell编程(25)——I/O 重定向
默认情况下始终有3个"文件"处于打开状态, stdin (键盘), stdout (屏幕), and stderr (错误消息输出到屏幕上). 这3个文件和其他打开的文件都可以被重 ...
- Delphi中WideString类型如何转化成String类型
var wstr:WideString; str:string; begin str:=WideCharToString(PWideChar(wstr)); end;
- del重复数
楼主 发表于: 2010-06-21 11:46:31 本帖最后由 luckycynthia 于 2010-06-21 11:47:46 编辑 在抓取数据后对数据进行操作的途中,有时候会碰到重复数据, ...
- Windows Server 2008 网站访问PHP响应慢的解决方法
公司新上了一个网站,但是在配置完PHP环境之后却发现了问题,访问HTML速度飞快,而访问PHP网页时就要卡顿1秒,响应很慢的样子,排除了带宽的因素之后,在百度上搜了一圈竟然解决了,现在将方法转载给大家 ...
- hdu 4411 arrest 最小费用流
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #i ...
- 介绍两个Eclipse插件: Implementors & Call Hierarchy
介绍两个Eclipse插件: Implementors & Call Hierarchy 本文介绍两个在Eclipse调试与跟踪过程中的两个实用插件 他们都可以在 http://eclipse ...
- a^b-b^a - SGU 112(高精度快速幂)
分析:直接上吧,建议不要使用模板,否则没啥意义了. 代码如下: ==================================================================== ...
- IOS 表视图UITableView 束NSBundle
今天搞了一下表视图UITableView 表视图是在以后应用程序开发中经常用到的一个视图,所以必须要熟练掌握 所获不多,对视图有了一个大概的了解 其中有用到NSBundle , 束 这个类 先说一 ...
- 使用BSD socket编写Windows版的网络程序
我们知道BSD Socket是标准的套接字规范,那么怎么在windows使用他们呢? 我们首先要引用<winsock2.h>和ws2_32.lib 然后,执行WSAStartup #ifd ...
- VMware-workstation-full-12.0.1-3160714
https://download3.vmware.com/software/wkst/file/VMware-workstation-full-12.0.1-3160714.exe 5A02H-AU2 ...