码迷,mamicode.com
首页 > 其他好文 > 详细

hdu-2169 Computer(树形dp+树的直径)

时间:2016-04-11 22:26:19      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:

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
 
5
1 1
2 1
3 1
1 1
 

 

Sample Output
 
3
2
3
4
4
 
题意:
 
给一棵树的边的情况,问树上每个点到其余点的最大距离是多少;
 
思路:
 
树上一点到其他点的最大距离的这条链一定至少有一个端点是直径的端点,所以,先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+树的直径)

标签:

原文地址:http://www.cnblogs.com/zhangchengc919/p/5380266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!