hdu 2196 Computer DP

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3651    Accepted Submission(s): 1852

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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;

typedef long long ll;
#define M 10005

vector<int>  adj[M],wi[M];
ll f[M][2];
int vis[M];

ll dfs1(int u){
    vis[u]=1;
    f[u][0]=0;

    for(int i=0;i<adj[u].size();i++){
        int v=adj[u][i];
        int w=wi[u][i];
        if(vis[v])   continue;

        f[u][0]=max(f[u][0],dfs1(v)+w);
    }
    return f[u][0];
}

void dfs2(int u){
    vis[u]=1;
    int m1=0,m2=0,v1,v2;

    for(int i=0;i<adj[u].size();i++){
        int v=adj[u][i];
        int w=wi[u][i];
        if(vis[v])  continue;

        int tmp=f[v][0]+w;
        if(tmp>m1){
            m2=m1,v2=v1;
            m1=tmp,v1=v;
        }
        else  if(m1==tmp || tmp>m2){
            m2=tmp,v2=v;
        }
    }

    if(u!=1){
        int tmp=f[u][1];
        int v=-1;
        if(tmp>m1){
            m2=m1,v2=v1;
            m1=tmp,v1=v;
        }
        else  if(m1==tmp || tmp>m2){
            m2=tmp,v2=v;
        }
    }

    for(int i=0;i<adj[u].size();i++){
        int v=adj[u][i];
        int w=wi[u][i];
        if(vis[v])  continue;

        if(v==v1)  f[v][1]=m2+w;
        else       f[v][1]=m1+w;
        dfs2(v);
    }
}

int main(){
    int n;
    while(cin>>n && n){
        for(int i=0;i<=n;i++)
            adj[i].clear(),wi[i].clear();
        for(int u=2;u<=n;u++){
            int v,w;
            cin>>v>>w;
            adj[u].push_back(v);
            wi[u].push_back(w);
            adj[v].push_back(u);
            wi[v].push_back(w);
        }

        memset(f,0,sizeof f);
        memset(vis,0,sizeof vis);
        dfs1(1);
        memset(vis,0,sizeof vis);
        dfs2(1);

        for(int j=1;j<=n;j++)
            cout<<max(f[j][0],f[j][1])<<endl;
    }
}
时间: 2024-10-10 00:11:50

hdu 2196 Computer DP的相关文章

HDU 2196 Computer 经典树形DP

一开始看错题了,后来发现原来是在一颗带权的树上面求出距离每一个点的最长距离,做两次dfs就好,具体的看注释? #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #incl

HDU 2196 Computer 树形DP经典题

链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问每台电脑和其它电脑的最远距离是多少. 思路:这是一道树形DP的经典题目.须要两次DFS,第一次DFS找到树上全部的节点在不同子树中的最远距离和次远的距离(在递归中进行动态规划就可以),第二次DFS从根向下更新出终于答案.对于每次更新到的节点u,他的最远距离可能是来自u的子树,或者是u的父亲节点的最远

树形DP [HDU 2196] Computer

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3526    Accepted Submission(s): 1788 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D

HDU 2196 Computer(树形DP求最长路)

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 a

题解报告:hdu 2196 Computer(树形dp)

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 a

hdu 2196 computer 树状dp

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3731    Accepted Submission(s): 1886 Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du

【树形DP】 HDU 2196 Computer

题意:求节点间的最大距离 先DFS一次 记录下 每一节点的子树下的最大距离(DP[ u ] [ 0 ])和第二大距离(DP[ u ] [ 1 ]) 用DP[ v ] [ 2 ] 表示由v的父节点来的最大距离 再取DP[ u ] [ 0 ] 与 DP[ u ][ 2 ] 的最值 #include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include <iostr

HDU 2196 Computer 树形DP 经典题

给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权,放在数组cost中 令tree(i)表示以节点i为根的子树 对于节点i,离该节点最远的点要不就是在tree(i)中,要不就是在father(i)上面 令: dp[i][1] : 在子树tree(i)中,离i最远的距离 dp[i][2] : 在子树tree(i)中,离i第二远的距离 (递推的时候需要)

HDU 2196——Computer(树形DP)

膜拜了NN个大神的代码,看了一整天,弱菜伤不起啊.求拜师啊 问题分析:求树上每个节点到其它节点的最远距离 每个节点到其它节点的最远距离就是以该节点为根的树所能达到的最大深度,这样子的话,要把每个节点转化为根,总共dfs的次数为节点数,肯定超时 于是~ 一个节点的最长路:1.从该节点往下取得最长路(子树部分)  2.从该节点往上取得的最长路(父节点往上的部分) 情况1:自下而上的dfs(先深搜后操作) 情况2:自上而下的dfs(先操作后深搜){ 如果节点u的子节点v在 以u为父亲的子树上的最长路,