zoukankan      html  css  js  c++  java
  • hdu2196 Computer[树形dp]

    Computer

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


    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
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1561 1011 3456 2242 2602 
     
    MX表示最大值

    mx表示次大值

    转移;1、其子树

       2、经过根

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    const int N=1e4+5;
    struct edge{int v,w,next;}e[N<<1];int tot,head[N];
    int MX[N],MAXid[N];
    int mx[N],maxid[N];
    int n,m;
    inline void add(int x,int y,int z){
        e[++tot].v=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot;
        e[++tot].v=x;e[tot].w=z;e[tot].next=head[y];head[y]=tot;
    }
    void PreDfs(int x,int f){
        MX[x]=mx[x]=0;
        for(int i=head[x],y;i;i=e[i].next){
            if((y=e[i].v)==f) continue;
            PreDfs(y,x);
            if(mx[x]<MX[y]+e[i].w){
                mx[x]=MX[y]+e[i].w;
                maxid[x]=y;
                if(mx[x]>MX[x]){
                    swap(MX[x],mx[x]);
                    swap(MAXid[x],maxid[x]);
                }
            }
        }
    }
    void SolDfs(int x,int f){
        for(int i=head[x],y;i;i=e[i].next){
            if((y=e[i].v)==f) continue;
            if(y==MAXid[x]){
                if(mx[y]<mx[x]+e[i].w){
                    mx[y]=mx[x]+e[i].w;
                    maxid[y]=x;
                    if(mx[y]>MX[y]){
                        swap(MX[y],mx[y]);
                        swap(MAXid[y],maxid[y]);
                    }
                }
            }
            else{
                if(mx[y]<MX[x]+e[i].w){
                    mx[y]=MX[x]+e[i].w;
                    maxid[y]=x;
                    if(mx[y]>MX[y]){
                        swap(MX[y],mx[y]);
                        swap(MAXid[y],maxid[y]);
                    }
                }
            }
            SolDfs(y,x);
        }
        
    }
    int main(){
        while(~scanf("%d",&n)){
            tot=0;memset(head,0,sizeof head);
            for(int i=2,y,z;i<=n;i++) scanf("%d%d",&y,&z),add(i,y,z);
            PreDfs(1,-1);
            SolDfs(1,-1);
            for(int i=1;i<=n;i++) printf("%d
    ",MX[i]);    
        }
        return 0;
    }
  • 相关阅读:
    电脑无法删除文件提示源路径太长怎么办|电脑由于文件名太长无法删除的解决方法
    史上最清晰的红黑树讲解(上)
    深入理解Java PriorityQueue
    为什么你的博客不够火?
    Java ArrayDeque源码剖析
    Java LinkedList源码剖析
    Java HashSet和HashMap源码剖析
    Java ArrayList源码剖析
    Java Collections Framework概览
    顺序对齐
  • 原文地址:https://www.cnblogs.com/shenben/p/6707018.html
Copyright © 2011-2022 走看看