• hdu2196 Computer


    Computer

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 30767    Accepted Submission(s): 3802


    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
     
    题目大意 求树上每个点到树上另一个点的最长距离
     
    题解
    树形dp 
    设一个点u到另一个点v,为u出发的最长距离。那么v要么在u的子树里,要么不在子树里...(废话!)
    dp[i][0]表示i到它子树的最深长度。dp[i][1]表示i到其子树的次深长度。dp[i][2]表示从i的爸爸出发到达的最深长度。
    那么dp[u][0]=max(dp[v][0]+edge[i].z)要推导出一个点到它子树的最深长度,必须要知道u这个点的儿子到他们子树
    的最深深度,所以dfs要倒着从根节点递推。而dp[i][2]是怎样推的呢。dp[v][2]=max(dp[u][2],dp[u][0]/dp[u][1])+edge[i].z;
    (u是v的爸爸)当v这个点是u到达子树最深节点路径上一个点时,那么dp[v][2]=max(dp[u][2],dp[u][1])+z,否则就是
    另一种情况。
    代码
     
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #define maxn 10005
    using namespace std;
    
    int n,y,w,sumedge;
    int head[maxn],dp[maxn][3];
    
    struct Edge{
        int x,y,z,nxt;
        Edge(int x=0,int y=0,int z=0,int nxt=0):
            x(x),y(y),z(z),nxt(nxt){}
    }edge[maxn];
    
    void add(int x,int y,int z){
        edge[++sumedge]=Edge(x,y,z,head[x]);
        head[x]=sumedge;
    }
    
    void dfs1(int x){
        for(int i=head[x];i;i=edge[i].nxt){
            int v=edge[i].y;
            dfs1(v);
            int gg=dp[v][0]+edge[i].z;
            if(gg>=dp[x][0]){
                dp[x][1]=dp[x][0];
                dp[x][0]=gg;
            }else if(gg>=dp[x][1])dp[x][1]=gg;
        }
    }
    
    void dfs2(int x){
        for(int i=head[x];i;i=edge[i].nxt){
            int v=edge[i].y;
            if(dp[x][0]==dp[v][0]+edge[i].z)
            dp[v][2]=max(dp[x][1],dp[x][2])+edge[i].z;
            else dp[v][2]=max(dp[x][2],dp[x][0])+edge[i].z;
            dfs2(v);
        }
    }
    
    int main(){
        while(~scanf("%d",&n)){
            memset(head,0,sizeof(head));
            memset(dp,0,sizeof(dp));
            sumedge=0;
            for(int i=2;i<=n;i++){
                scanf("%d%d",&y,&w);
                add(y,i,w);
            }
            dfs1(1);
            dfs2(1);
            for(int i=1;i<=n;i++)
             printf("%d
    ",max(dp[i][0],dp[i][2]));
        }
        return 0;
    }
  • 相关阅读:
    Ubuntu设置默认编辑器
    Java基础学习之(15)
    Java基础学习之(9)--java的常用类
    Java基础学习之(11)--异常处理
    Java基础学习之(10)--接口
    Java基础学习之(8)--多态和抽象类
    Java基础学习之(7)--Object类
    Java基础学习之(6)--继承、访问权限、重写
    dubbo-整合springboot、基于注解的简单实例
    java安全框架之Permission学习笔记
  • 原文地址:https://www.cnblogs.com/zzyh/p/7476125.html
Copyright © 2020-2023  润新知