HDU 2196 - Computer (树上每点最长距离 DP)

本文介绍了一种解决树形结构中寻找从每个节点出发的最长路径问题的算法。通过两次深度优先搜索(DFS),首先确定每个节点仅通过其子节点所能达到的最长和次长路径,然后更新包括父节点在内的最长路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2960    Accepted Submission(s): 1501



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   |   We have carefully selected several similar problems for you:   1561  1011  3456  1520  2242 
 


题意:

给一棵树,求树上每个点出发的最长距离



思路:

i点最长路dp[i]=max(dp[j]+w[i][j]) j为i的孩子或者i的父亲

而i的父亲的最长路有可能来自i,所以要记录每个点的最长路和次长路以及这两条路分别来自哪个点


第一遍DFS求出所有点只通过孩子节点得到的最长路和次长路

第二遍DFS求出所有点来自父亲和孩子节点的最长路的最大值


问题:

第二遍DFS的时候忘了考虑来自父亲的路径小于最长而大于次长


样例:


input :
10
1 1
1 2
1 3
2 3
3 4
3 1
6 2
6 4
9 5
output :
15
16
13
18
19
10
14
12
14
19




#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 10000 + 20;
struct Node {
    int v, w;
    Node(int vv=0, int ww=0) : v(vv), w(ww) {}
};

vector<Node> G[maxn];
int maxD[maxn], maxDi[maxn];
int smaxD[maxn], smaxDi[maxn];

int dfs1(int u) {
    maxD[u] = smaxD[u] = 0;
    int sonn = G[u].size();
    for(int i=0; i<sonn; i++) {
        int v = G[u][i].v;
        int w = G[u][i].w;
        int ret = dfs1(v) + w;
        if(ret >= maxD[u]) {
            smaxD[u] = maxD[u];
            smaxDi[u] = maxDi[u];
            maxD[u] = ret;
            maxDi[u] = v;
        } else if(ret >= smaxD[u]) {
            smaxD[u] = ret;
            smaxDi[u] = v;
        }
    }
    return maxD[u];
}

void dfs2(int u) {
    int sonn = G[u].size();
    for(int i=0; i<sonn; i++) {
        int v = G[u][i].v;
        int w = G[u][i].w;
        int tf = maxDi[u] != v ? maxD[u] : smaxD[u];
        tf += w;
        if(tf >= maxD[v]) {
            smaxD[v] = maxD[v];
            smaxDi[v] = maxDi[v];
            maxD[v] = tf;
            maxDi[v] = u;
        } else if(tf > smaxD[v]) {
            smaxD[v] = tf;
            smaxDi[v] = u;
        }
        dfs2(v);
    }
}

int main() {
    int n;

    while(scanf("%d", &n) != EOF) {
        for(int i=1; i<=n; i++) G[i].clear();
        for(int i=2; i<=n; i++) {
            int u, w;
            scanf("%d%d", &u, &w);
            G[u].push_back(Node(i, w));
        }
        dfs1(1);
        dfs2(1);
        for(int i=1; i<=n; i++) {
            printf("%d\n", maxD[i]);
        }
    }

    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值