HDU-2196-Computer (DP tree Classic title)

Computer

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.

Here Insert Picture Description

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

Problem-solving ideas:

There pits subject to multiple sets of input.
Given a tree, the right side of each value.
Each request can reach the most remote point.
DFS can twice.
DFS first pass every single subtree is determined for each point the maximum length. (Only use first two long two)
second pass DFS can be solved answer. Pass two parameters. Wherein a is the longest to reach the above parent node passed down. Then down DFS, DFS down time to consider how to choose the parameters, two long distance before the selection layer is passed down from that point. Plus the right to choose the maximum value of the point spread down the line. Then record the answers of this point. And also the level value takes a maximum value subtree maximum value on the line.

AC Code:

//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
#include <queue>
#include <deque>
#define ios std::ios::sync_with_stdio(false)
#define int long long
using namespace std;
const int N = 3e5+10;
struct node{
    int val;
    vector<int> to;
    vector<pair<int,int> > dis;
}nodes[N];
int dp[N];

int dfs(int pos)
{
    int res = 0;
    for(int i = 0 ; i < nodes[pos].to.size() ; i ++)
    {
        int tmp = dfs(nodes[pos].to[i]);
        nodes[pos].dis.push_back(make_pair(nodes[pos].to[i],tmp));
        res = max(res,tmp);
    }
    return res + nodes[pos].val;
}

bool cmp(pair<int,int> x,pair<int,int> y)
{
    return x.second > y.second;
}

void dfs2(int pos,int sum)
{
    sort(nodes[pos].dis.begin(),nodes[pos].dis.end(),cmp);
    int dis1 = 0,dis2 = 0;
    int to1 = -1;
    if(nodes[pos].to.size() > 1) {dis1 = nodes[pos].dis[0].second; to1 = nodes[pos].dis[0].first; dis2 = nodes[pos].dis[1].second;}
    if(nodes[pos].to.size() > 0) {dis1 = nodes[pos].dis[0].second; to1 = nodes[pos].dis[0].first;}
    // cout<<pos<<" "<<sum<<" "<<dis1<<" "<<dis2<<endl;
    for(int i = 0 ; i < nodes[pos].to.size() ; i ++)
    {
        dfs2(nodes[pos].to[i],max(sum,(to1 == nodes[pos].to[i] ? dis2 : dis1))+nodes[nodes[pos].to[i]].val);
    }
    dp[pos] = max(sum,dis1);
}

signed  main()
{
    int n;
    while(cin>>n){
        for(int i = 1 ; i <= n ; i ++)
        {
            nodes[i].to.clear();
            nodes[i].dis.clear();
        }
        for(int i = 2 ; i <= n; i ++)
        {
            int x,y;
            cin>>x>>y;
            nodes[x].to.push_back(i);
            nodes[i].val = y;
        }
        nodes[1].val = 0;
        dfs(1);
        dfs2(1,0);
        for(int i =1 ; i <= n ; i ++)
            cout<<dp[i]<<endl;
    }
    return 0;
}

Published 104 original articles · won praise 7 · views 4061

Guess you like

Origin blog.csdn.net/qq_43461168/article/details/104210248