HDU 2196 Computer 樹形DP經典題

來源:互聯網
上載者:User

標籤:dp   樹形dp   

連結:http://acm.hdu.edu.cn/showproblem.php?pid=2196

題意:每個電腦都用線串連到了另一台電腦,串連用的線有一定的長度,最後把所有電腦連成了一棵樹,問每台電腦和其他電腦的最遠距離是多少。

思路:這是一道樹形DP的經典題目,需要兩次DFS,第一次DFS找到樹上所有的節點在不同子樹中的最遠距離和次遠的距離(在遞迴中進行動態規劃即可),第二次DFS從根向下更新出最終答案,對於每次更新到的節點u,他的最遠距離可能是來自u的子樹,或者是u的父親節點的最遠距離,如果u的父親節點的最遠距離是在第一次DFS過程中更新自u的話,那麼u的最遠距離就不能更新自u的父親節點的最遠節點,而是有可能更新自u的父親節點的次遠距離,這就是每次更新時要記錄節點的次遠距離的原因。

代碼:

#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 10005#define PI acos(-1.0)#define seed 31//131,1313typedef long long LL;typedef unsigned long long ULL;using namespace std;int dp[maxn][2],from[maxn],head[maxn],top;void init(){    memset(head,-1,sizeof(head));    memset(dp,0,sizeof(dp));    memset(dp,0,sizeof(dp));    top=0;}struct Edge{    int v,w;    int next;} edge[maxn*2];void add_edge(int u,int v,int w){    edge[top].v=v;    edge[top].w=w;    edge[top].next=head[u];    head[u]=top++;}void dfs_first(int u,int f){    from[u]=u;    for(int i=head[u]; i!=-1; i=edge[i].next)    {        int v=edge[i].v,w=edge[i].w;        if(v==f)            continue;        dfs_first(v,u);        if(dp[v][0]+w>dp[u][0])        {            from[u]=v;            dp[u][1]=dp[u][0];            dp[u][0]=dp[v][0]+w;        }        else if(dp[v][0]+w>dp[u][1])            dp[u][1]=dp[v][0]+w;    }}void dfs_second(int u,int f,int k){    if(u!=f)        if(from[f]!=u)        {            if(dp[f][0]+k>dp[u][0])            {                from[u]=f;                dp[u][1]=dp[u][0];                dp[u][0]=dp[f][0]+k;            }            else if(dp[f][0]+k>dp[u][1])                dp[u][1]=dp[f][0]+k;        }        else        {            if(dp[f][1]+k>dp[u][0])            {                from[u]=f;                dp[u][1]=dp[u][0];                dp[u][0]=dp[f][1]+k;            }            else if(dp[f][1]+k>dp[u][1])                dp[u][1]=dp[f][1]+k;        }    for(int i=head[u]; i!=-1; i=edge[i].next)    {        int v=edge[i].v,w=edge[i].w;        if(v==f)            continue;        dfs_second(v,u,w);    }}int main(){    int T,v,w;    while(~scanf("%d",&T))    {        init();        for(int i=2; i<=T; i++)        {            scanf("%d%d",&v,&w);            add_edge(v,i,w);            add_edge(i,v,w);        }        dfs_first(1,1);        dfs_second(1,1,0);        for(int i=1;i<=T;i++)            printf("%d\n",dp[i][0]);    }    return 0;}


HDU 2196 Computer 樹形DP經典題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.