hdu-2169 Computer(樹形dp+樹的直徑)

來源:互聯網
上載者:User

標籤:

題目連結:

Computer

Time Limit: 1000/1000 MS (Java/Others)   

 Memory Limit: 32768/32768 K (Java/Others)


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 51 12 13 11 1 

 

Sample Output 32344  題意: 給一棵樹的邊的情況,問樹上每個點到其餘點的最大距離是多少;  思路: 樹上一點到其他點的最大距離的這條鏈一定至少有一個端點是直徑的端點,所以,先dfs找到一個端點;再來一遍bfs找到所有點到這個端點的距離,這些距離可能就是最後的答案,也可能不是,然後再從直徑的另一個端點出發,找到所有點到這個端點的距離,和上個距離取最大就是結果了;  AC代碼:
/*    2196    15MS    1976K    2205 B    G++    2014300227*/#include <bits/stdc++.h>using namespace std;const int N=1e4+4;typedef long long ll;const double PI=acos(-1.0);int n,vis[N],head[N],cnt,a,b,dp[N],fp[N],mmax,ending;queue<int>qu;struct Edge{    int to,next,val;};Edge edge[2*N];void add_edge(int s,int e,int va){    edge[cnt].to=e;    edge[cnt].next=head[s];    edge[cnt].val=va;    head[s]=cnt++;}void dfs(int x,int leng){    if(leng>mmax)    {        ending=x;        mmax=leng;    }    vis[x]=1;    for(int i=head[x];i!=-1;i=edge[i].next)    {        int y=edge[i].to;        if(!vis[y])        {            dfs(y,leng+edge[i].val);        }    }}void bfs1(){    qu.push(ending);    dp[ending]=0;    vis[ending]=0;    while(!qu.empty())    {        int fr=qu.front();        qu.pop();        for(int i=head[fr];i!=-1;i=edge[i].next)        {            int y=edge[i].to;            if(vis[y])            {                dp[y]=dp[fr]+edge[i].val;                qu.push(y);                vis[y]=0;            }        }    }}void bfs2(){    int start,mmx=0;    for(int i=1;i<=n;i++)    {        if(dp[i]>mmx)        {            start=i;            mmx=dp[i];        }    }    qu.push(start);    fp[start]=0;    vis[start]=1;    while(!qu.empty())    {        int fr=qu.front();        qu.pop();        for(int i=head[fr];i!=-1;i=edge[i].next)        {            int y=edge[i].to;            if(!vis[y])            {                fp[y]=fp[fr]+edge[i].val;                dp[y]=max(dp[y],fp[y]);                vis[y]=1;                qu.push(y);            }        }    }}int main(){    while(scanf("%d",&n)!=EOF)    {        for(int i=1;i<=n;i++)        {            vis[i]=0;            head[i]=-1;        }        cnt=0;        mmax=0;        for(int i=2;i<=n;i++)        {            scanf("%d%d",&a,&b);            add_edge(i,a,b);            add_edge(a,i,b);        }        dfs(1,0);        bfs1();        bfs2();        for(int i=1;i<=n;i++)        {            printf("%d\n",dp[i]);        }    }    return 0;}

 

  

hdu-2169 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.