I am a slow walker,but I never walk backwards. Abraham Lincoln

GeekZRF

HDU 2196.Computer 树形dp 树的直径

Computer

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


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
 
题意:求一棵树上任一点到其他点的最远距离。
思路:1)树上的任意一点到其他点的最远距离必然是到达树的直径的两个端点之一,树的直径只需要2遍dfs即可。
2)重点说一下树形dp的思路。dp[i][0],dp[i][1],dp[i][2]分别表示:i通过子树可以达到的最远距离;i通过子树可以到达的次远距离(不同儿子);i通过父亲能够到达的最远距离。那么ans[i]=max(dp[i][0],dp[i][2]);
对于<u,v>w:
状态转移方程:dp[u][0]=dp[v][0]+w;dp[u][1]也是这样得到,但是注意dp[i][1]是通过另外的儿子获得。
1.dp[v][2]=dp[u][1]+w; 2.dp[v][2]=dp[u][0]+w;当<u,v> 是u的最长子树的路径时,只能用1式子。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int > P;
const int N=1e5+100,M=1e5+100;
const int inf=0x3f3f3f3f;
const ll INF=1e18+7,mod=1e9+7;
struct edge
{
    int from,to;
    ll w;
    int next;
};
edge es[M];
int cnt,head[N];
ll dp[N][5];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,ll w)
{
    cnt++;
    es[cnt].from=u,es[cnt].to=v;
    es[cnt].w=w;
    es[cnt].next=head[u];
    head[u]=cnt;
}
void dfs1(int u,int fa)
{
    for(int i=head[u]; i!=-1; i=es[i].next)
    {
        edge e=es[i];
        if(e.to==fa) continue;
        dfs1(e.to,u);
        ll d=dp[e.to][0]+e.w;
        if(d>dp[u][0]) swap(d,dp[u][0]);
        if(d>dp[u][1]) swap(d,dp[u][1]);
    }
}
void dfs2(int u,int fa)
{
    for(int i=head[u]; i!=-1; i=es[i].next)
    {
        edge e=es[i];
        if(e.to==fa) continue;
        if(dp[u][0]==dp[e.to][0]+e.w)
            dp[e.to][2]=max(dp[u][2],dp[u][1])+e.w;
        else
            dp[e.to][2]=max(dp[u][2],dp[u][0])+e.w;
        dfs2(e.to,u);
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        init();
        for(int i=2,j; i<=n; i++)
        {
            ll w;
            scanf("%d%lld",&j,&w);
            addedge(i,j,w);
            addedge(j,i,w);
        }
        memset(dp,0,sizeof(dp));
        dfs1(1,0);
        dfs2(1,0);
        for(int i=1; i<=n; i++)
            printf("%d\n",max(dp[i][0],dp[i][2]));
    }
    return 0;
}
树形dp

 

posted on 2017-09-29 13:48  GeekZRF  阅读(238)  评论(0编辑  收藏  举报

导航