hdu2196 Computer

来源:互联网 发布:sql语句面试题及答案 编辑:程序博客网 时间:2024/05/22 14:20
HomeProblStatusContest  

-13:16:30
  • Overview
  • Problem
  • Status
  • Rank
A              
B - Computer
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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

51 12 13 11 1
 

Sample Output

32344
又是一个很经典的树状dp,在这题里,意思就是,结一个树,把每个结点到其他结点的最大距离输出来,我们先明
确三个概念,第一长距离,是指,该结点通过子结点中的点能达到的最长距离,第二长距离是指,通过子结点通达
到的刚好比第一长距离小一点点,也就是排第二长的距离,父结点的距离,指必须通过父结点,可以到达的最长距离
我们以这个例子来说,我们可以从叶到根先找到最长和第二长,再通过从根到叶,最长和第二长所经由的路找到通过
父结点,所能达到的最长距离,而最终的结果,一定是最长和父结点距离中的最大值所确定的,你画图就知道,最长路
不是通过子结点中的,就是父结点中的最大值
1 4
1 1
3 3
3 6
这个情况基本上把所有容易掉的情况展显了,其中4 5号结点是最特殊的,4号结点,必须通过父结点达到最大
5号必须通过父结点的达到最大
#include <iostream>#include <stdio.h>#include <vector>#include<string.h>using namespace std;struct computertree{int next ,val ;};vector <computertree> vec[10050];int n;int firstpath[10050];//分别记录,当前结点的,第一长和第二长的距离通过的子结点的编号int dp[10050],dpsecond[10050],dpfather[10050];//分别记录第一长,第二长,和从父结点过来的最长的距离int fmax(int a,int b){    if(a>b)    return a;    return b;}void init(){    int i,e;    computertree temp;    memset(dp,0,sizeof(dp));    memset(dpsecond,0,sizeof(dpsecond));    memset(dpfather,0,sizeof(dpfather));    memset(firstpath,-1,sizeof(firstpath));    for(i=1;i<=n;i++)    {        vec[i].clear();    }    for(i=2;i<=n;i++)    {        scanf("%d%d",&e,&temp.val);        temp.next=i;        vec[e].push_back(temp);    }}void dfsup(int x)//从叶子结点向根来建树,得到,最大,和第二大的距离{    int i,j;    int  flag1=-1,flag2=-1,maxx;//标记所取最长距离的点    if(vec[x].size()==0)//达到叶子结点返回    return ;    for(i=0;i<vec[x].size();i++)    {        j=vec[x][i].next;        dfsup(j);//不断的向子结点进,直到达到了叶子结点        if(dp[j]+vec[x][i].val>dp[x])        {            dp[x]=dp[j]+vec[x][i].val;            flag1=j;//标记最大距离通向的结点        }    }    firstpath[x]=flag1;    for(i=0;i<vec[x].size();i++)    {        j=vec[x][i].next;        if(j!=flag1&&dp[j]+vec[x][i].val>dpsecond[x])//和第一不相等的子结点,就是第二大的子结点        {            dpsecond[x]=dp[j]+vec[x][i].val ;//更新第二长的距离的大小            flag2=i;        }    }  }void dfsdown(int x)//从根结点,向下进行更新,获取最大值 {    int i,j,tempval;    for( i=0;i<vec[x].size();i++)    {        j=vec[x][i].next;        tempval=vec[x][i].val;        if(firstpath[x]!=j)//父结点的最长路不经过子结点,且距离大于子结点的最长路,就要进行更新        {            dpfather[j]=fmax(dpfather[x],dp[x])+tempval;//对应4号结点情况        }        else if(firstpath[x]==j)        {           dpfather[j]=fmax(dpfather[x],dpsecond[x])+tempval;//注意1对应5号结点        }        dfsdown(j);//向子结点扩展    }}int main(){    int i;    while(scanf("%d",&n)!=EOF)    {        init();        dfsup(1);        dfsdown(1);        for(i=1;i<=n;i++)        {            printf("%d\n",fmax(dpfather[i],dp[i]));        }    }    return 0;}


 

FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
All Copyright Reserved ©2010-2012 HUST ACM/ICPC TEAM 
Anything about the OJ, please ask in the forum, or contact author:Isun
Server Time: