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,p的次远距离+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

时间: 2024-08-01 21:07:38

SGU 149. Computer Network的相关文章

SGU 149. Computer Network( 树形dp )

题目大意:给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) ----------------------------------------------------------

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

J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” consists of n servers and m two-way communication links. Two servers can communicate either thr

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

题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” consists of n servers and m two-way communication links. Two servers can communicate either through a direct link, or through a chain of links, by relayi

SGU 149 树形DP Computer Network

这道题搜了一晚上的题解,外加自己想了半个早上,终于想得很透彻了.于是打算好好写一写这题题解,而且这种做法比网上大多数题解要简单而且代码也比较简洁. 首先要把题读懂,把输入读懂,这实际上是一颗有向树.第i(2≤i≤n)行的两个数u,d,其中u是i的父亲结点,d是距离. 第一遍DFS我们可以计算出以u为根的子树中,距离u最远的结点的距离d(u, 0)以及次远的距离d(u, 1).而且,这两个不在u的同一棵子树中,如果u只有一个孩子,那么d(u, 1) = 0 第一遍DFS完以后,因为1是整棵树的跟,

Computer Network学习笔记_2

1_5 Traceroute,一种command tool,可以看network内部信息,ISP内部信息. 1_6 理解Network的模块化封装.学习Protocols和Layers,这种构建computer networks的关键机制.封装是越底层的protocol越封装在外面,形成一个protocol stack.每一层都在message加自己的header.当Browser和Server传输信息时,从Brower向下逐层封装,通过物理层传输,再从下到上demultiplexing.在不同

Computer Network学习笔记_1

1_1 开始学习David Weatherall讲的Computer Networks,第一节课主要讲Goals and Motivation,Focus of the course是Networking,讨论packets and internet,会讲底层的Communications和上层的Distributed systems,就是Network如何建立,上层apps能做什么.The main point是学习Internet如何运行,TCP/IP,DNS,HTTP,NAT,VPNs,8

computer network fundamental

Copy from  http://cyberlingo.blogspot.com/2015/10/data-communications-and-networking-2.html List the layers of the Internet model. Physical, Data Link, Network, Transport, Application. Which layers in the Internet model are the network support layers

Computer Network学习笔记_4

3-2_Retransmissions 主讲ARQ,主要用在错误普遍存在而且必须改正的情况,WiFi.TCP都用.ARQ的规则就是recerver收到正确帧要自动回发ACK,sender如果没有在timeout内收到ACK就重发Frame.ARQ有两个问题,一个是timeout定多长,一个是帧重复.Timeout定的时间不能太长太短,解决帧重复就是在Frames和ACKs中加sequence numbers.帧重复的问题是如果sender发的数据receiver收到然后回发的ACK超时,那么se

Computer Network学习笔记_5

4-1_Network_Layer_Overview 一个知识点:routing和forwarding的区别.Routing is the process of deciding in which direction to send traffic-Network wide(global)and expensive.Forwarding is the process of sending a packet on its way-Node process(local) and fast. 因为网络