hdu2196-Computer(树形dp)

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

解析: 要找节点的最大距离有两种情况,一种是往子树
方向走的最大距离,另一种是往上走经过父节点的最大距离。
这里可以两遍dfs,一遍搜的过程中保存最大距离和次大距离
以及最大距离路径选取的儿子。第二遍搜是通过dp得到往上走
经过父节点能走的最远距离,如果此节点是父节点最大距离路径
上的儿子,则考虑父节点的次大值和dp值中的最大值加上两点的
距离。否则考虑父节点的最大值和dp值中的最大值加上两点的距离。

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=10005;
struct edge
{
    int v,w;
    edge(int v=0,int w=0):v(v),w(w){}
};
vector<edge> G[maxn];
int N,fm[maxn],smax[maxn],son[maxn],dp[maxn];
//fm是往子树走最大距离,smax是往子树走次大距离,
//son是往子树走最大距离路径上的儿子
//dp[i]表示点i往上走能到达的最远距离
void init()
{
    for(int i=1;i<=N;i++)
    {
        G[i].clear();
        fm[i]=smax[i]=0;
        dp[i]=0;
        son[i]=-1;
    }
}
int dfs(int u)
{
    if(fm[u]) return fm[u];
    int Size=G[u].size();
    if(!Size) return 0;
    int a=-1,b;
    for(int i=0;i<Size;i++)
    {
        edge& e=G[u][i];
        int v=e.v,w=e.w;
        dfs(v);
        if(fm[v]+w>a) { a=fm[v]+w; b=v; } //更新最大值和儿子
    }
    fm[u]=a;
    son[u]=b;
    a=-1;
    for(int i=0;i<Size;i++)
    {
        edge& e=G[u][i];
        int v=e.v,w=e.w;
        if(v==son[u]) continue;
        if(fm[v]+w>a) a=fm[v]+w; //次大
    }
    if(a!=-1) smax[u]=a;
    return fm[u];
}
void DP(int u) 
{
    int Size=G[u].size();
    for(int i=0;i<Size;i++)
    {
        edge& e=G[u][i];
        int v=e.v,w=e.w;
        if(v==son[u]) dp[v]=max(dp[u],smax[u])+w; //选取次大和父节点往上走的最远距离
        else dp[v]=max(dp[u],fm[u])+w; //选取最大和父节点往上走的最远距离
        DP(v);
    }
    return;
}
int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        init();
        int u,w;
        for(int v=2;v<=N;v++)
        {
            scanf("%d%d",&u,&w);
            G[u].push_back(edge(v,w));//邻接表
        }
        fm[1]=dfs(1);
        DP(1);
        for(int i=1;i<=N;i++) printf("%d
",max(dp[i],fm[i]));
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/wust-ouyangli/p/5780393.html