hdu2196-Computer

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 32652    Accepted Submission(s): 4625


Problem Description
A school bought the first computer some time ago(so this computer's id is 1). 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 the maximum distance Si 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. 


Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
 

Input
Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, 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
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
 

Sample Input
 
 
5 1 1 2 1 3 1 1 1
 

Sample Output
 
 
3 2 3 4 4
 

Author
scnu
 

Recommend

lcy

题意:有n个电脑,n - 1条边连接,每条边有个权值,问离每个电脑最远的电脑距离是多少

题解:对于任何一个点,要得到离它最远的点可以直接暴力求解。但这样做果断TLE。

现在我们先假设这是一个有根树,则从根节点出发往叶子节点遍历,能得到每个节点到其所有子树的叶子节点的最大距离。

再考虑,子节点到父节点再从父节点到其它子节点的情况。我们再从根节点出发,对于当前节点的子节点,取不经过该子节点的最大的路径,加上父子节点之间的长度,更新子节点到父节点的距离,再与子节点的dis比较,取较大值。

即本题两边dfs即可得出答案。

AC代码

#include <stdio.h>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>
typedef long long ll;
 
using namespace std;

const ll maxn = 1e4 + 10;
ll vis[maxn], dis[maxn];

struct node{
	ll id, len, d;
	node(ll i, ll l, ll dx):id(i), len(l), d(dx){}
};
vector<node> s[maxn];

ll dfs(ll x){
	ll len = s[x].size();
	ll nn = 0;
	for(ll i = 0; i < len; i++){
		ll v = s[x][i].id;
		ll l = s[x][i].len;
		if(!vis[v]){
			vis[v] = true;
			ll k = dfs(v) + l;
			nn = max(nn, k);
			s[x][i].d = k;
		}
	}
	dis[x] = nn;
	return nn;
}

bool cmp(node a1, node a2){
	return a1.d > a2.d;
}

void dfs1(ll x){
	ll len = s[x].size();
	for(ll i = 0; i < len; i++){
		ll v = s[x][i].id;
		ll l = s[x][i].len;
		if(!vis[v]){
			vis[v] = true;
			ll cc = 0;
			if(i == 0){
				if(len != 1)
					cc = s[x][1].d + l;
				else
					cc = l;
			}
			else
				cc = s[x][0].d + l;
			dis[v] = max(dis[v], cc);
			ll len1 = s[v].size();
			for(ll j = 0; j < len1; j++){
				if(s[v][j].id == x){
					s[v][j].d = cc;
					break;
				}
			}
			sort(s[v].begin(), s[v].end(), cmp);
			dfs1(v);
		}
	}
}

int main(){
	ll n, a, b;
	while(scanf("%lld", &n) != EOF){
		for(ll i = 0; i <= n; i++){
			s[i].clear();
			vis[i] = 0;
		}
		for(ll i = 0; i < n - 1; i++){
			scanf("%lld %lld", &a, &b);
			s[i + 2].push_back(node(a, b, 0));
			s[a].push_back(node(i + 2, b, 0));
		}
		vis[1] = 1;
		dfs(1);
		for(ll i = 0; i <= n; i++){
			vis[i] = 0;
			sort(s[i].begin(), s[i].end(), cmp);
		}
		vis[1] = 1;
		dfs1(1);
		for(ll i = 1; i <= n; i++)
			printf("%lld\n", dis[i]);
	}
	return 0;
} 
树形dp第二发,蒟蒻做题慢死了,水题要做一个多小时,哭晕在厕所。。。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值