zoukankan      html  css  js  c++  java
  • Evanyou Blog 彩带

      题目传送门

    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. 



    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
     

      分析:

      一道经典的树形DP。

      每次从需要求的节点出发,然后遍历该树,期间更新该点的最长路。但是用DP数组存最长路的时候有一个问题,就是便利的方向,如果不考虑方向的话肯定是错的,那么可以转换一下,DP数组不存点的最长路,存边的最长路,这样使用邻接链表村边的化就可以考虑到方向了。具体看代码。

      Code:

    //It is made by HolseLee on 24th July 2018
    //HDU2196
    #include<bits/stdc++.h>
    using namespace std;
    
    const int N=1e4+7;
    int n,dp[N<<1],head[N],size;
    struct Node{
        int to,next,val;
    }edge[N<<1];
    
    inline int read()
    {
        char ch=getchar();int num=0;bool flag=false;
        while(ch<'0'||ch>'9'){if(ch=='-')flag=true;ch=getchar();}
        while(ch>='0'&&ch<='9'){num=num*10+ch-'0';ch=getchar();}
        return flag?-num:num;
    } 
    
    inline void add(int x,int y,int z)
    {
        edge[++size].to=y;
        edge[size].val=z;
        edge[size].next=head[x];
        head[x]=size;
    }
    
    inline int dfs(int x,int las)
    {
        int ret=0;
        for(int i=head[x];i!=-1;i=edge[i].next){
            int y=edge[i].to;
            if(y==las)continue;
            if(!dp[i]){
                dp[i]=max(dp[i],dfs(y,x)+edge[i].val);
            }
            ret=max(ret,dp[i]);
        }
        return ret;
    } 
    
    ;int main()
    {
        while(scanf("%d",&n)!=EOF){
            int x,y;
            memset(dp,0,sizeof(dp));
            memset(head,-1,sizeof(head));
            size=0;
            for(int i=1;i<n;i++){
                x=read();y=read();
                add(i+1,x,y);add(x,i+1,y);
            }
            for(int i=1;i<=n;i++)
            printf("%d
    ",dfs(i,i));
        }
    }
  • 相关阅读:
    gulp之压缩合并MD5清空替换加前缀以及自动编译自动刷新浏览器大全
    HTML5之文件API
    Angular2之路由学习笔记
    nodejs之主机不能访问到虚拟机的web服务器
    学习CSS3动画(animation)
    jQuery之ajax错误调试分析
    Angular2之管道学习笔记
    css3之3D魔方动画(小白版)
    关于二维网格导入autodyn的问题
    两个橡胶球自由落体撞击弹性板
  • 原文地址:https://www.cnblogs.com/cytus/p/9362540.html
Copyright © 2011-2022 走看看