现在的位置: 首页 > 综合 > 正文

HDU 2196 Computer – from lanshui_Yang

2019年01月08日 ⁄ 综合 ⁄ 共 2638字 ⁄ 字号 评论关闭
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
题目大意:抽象以后的问题很容易理解:给你一棵树,让你求出树中任意一个节点到其他节点的最大距离。
解题思路:简单的想法是对每个节点都调用一次bfs来求和树中其他节点的距离的最大值,但是,这显然会TLE。换种思路,先求出树的直径的两个端点A 和 B , 那么树中任意一个节点C 在树中距离其他节点的最大值一定是max{A -> C , B -> C} ,这样,只需调用3次 bfs 就可以了,第一次以任意节点为起点进行bfs找端点A , 第二次是以端点A为起点进行 bfs 找端点B(此过程中同时求出了树中其他节点到端点A的距离的值 ) , 第三次是以端点B为起点进行bfs , 此过程是为了求出树中其他节点到端点 B 的距离的值 同时 求出max{A -> C , B -> C} 。
下面请看代码:
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std ;
const int MAXN = 1e6 +7 ;
int n ;
struct Node
{
    int adj ;
    int dist ;
    Node *next ;
};
Node *vert[MAXN] ;
int vis[MAXN] ; // 建立标记数组
int distmp[MAXN] ;   // 记录端点A到树中其他节点的距离
int disans[MAXN] ;  // 记录 max{A -> C  , B -> C}
queue<int> q ;
int bfs(int start)
{
    memset(vis , 0 , sizeof(vis)) ;  // 每次 bfs 都别忘记
    memset(distmp , 0 , sizeof(distmp)) ;
    while (!q.empty())
    {
        q.pop() ;
    }
    int duan ;
    vis[start] = 1 ;
    distmp[start] = 0 ;
    if(distmp[start] > disans[start])  // 找max{A -> C  , B -> C}
    {
        disans[start] = distmp[start] ;
    }
    q.push(start) ;
    int maxr = 0 ;
    Node * p ;
    int tmp ;
    while (!q.empty())
    {
        tmp = q.front() ;
        q.pop() ;
        vis[tmp] = 1 ;
        p = vert[tmp] ;
        while (p != NULL)
        {
            int tp2 = p -> adj ;
            if(!vis[tp2])
            {
                vis[tp2] = 1 ;
                distmp[tp2] = distmp[tmp] + p -> dist ;
                if(distmp[tp2] > disans[tp2])
                {
                    disans[tp2] = distmp[tp2] ;
                }
                if(maxr < distmp[tp2])
                {
                    duan = tp2 ;
                    maxr = distmp[tp2] ;
                }
                q.push(tp2) ;
            }
            p = p -> next ;
        }
    }
    return duan ;
}
void ans()  // 进行3次bfs
{
    int duan1 = bfs(1) ;
    int duan2 = bfs(duan1) ;
    bfs(duan2) ;
}
int main()
{
    while (scanf("%d" , &n) != EOF)
    {
        memset(disans , 0 , sizeof(disans)) ;
        memset(vert , 0 , sizeof(vert)) ;
        int i ;
        getchar() ;
        for(i = 2 ; i <= n ; i ++)  // 建图 ,此处很关键,千万要理解题意!!
        {
            int b , d ;
            scanf("%d%d" , &b , &d) ;
            Node *p ;
            p = new Node ;
            p -> adj = b ;
            p -> dist = d ;
            p -> next = vert[i] ;
            vert[i] = p ;

            p = new Node ;
            p -> adj = i ;
            p -> dist = d ;
            p ->next = vert[b] ;
            vert[b] = p ;
        }
        ans() ;
        for(i = 1 ; i <= n ; i ++)
        {
            printf("%d\n" , disans[i]) ;
        }
    }
    return 0 ;
}

抱歉!评论已关闭.