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;
    }
  • 相关阅读:
    you don't have permission to access forbidden
    SQL优化之语句优化
    MySQL索引介绍
    com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:D:developmentideaProjectssalary-card argetsalary-card-0.0.1-SNAPSHOT.jar!BOOT-INFclasses!keystorelogin_id_rsa 资源未找到
    Java 原生日志 java.util.logging
    为什么volatile不能保证原子性?
    SpringCloud-微服务配置统一管理SpringCloud Config(七)
    SpringCloud-微服务网关ZUUL(六)
    SpringCloud-容错处理Hystrix熔断器(五)
    SpringCloud-声明式Rest调用Feign(四)
  • 原文地址:https://www.cnblogs.com/shenben/p/6707018.html
Copyright © 2011-2022 走看看