HDU Problem 2196 Computer【树的直径】

Computer

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1561 1011 3456 1520 2242 
 
根据树的直径的证明知道,从树上的任意一点到达的最远距离必定是树的两个端点,所以先BFS一个端点 ,求出各个节点到这个短点的距离,再bfs另外一个端点,则树上的节点到这两个短点的最大值为这个节点在树上可求得最大距离。

#include <bits/stdc++.h>
#define MAXN 10010
using namespace std;
struct node{
    int from, to, val, next;
} edge[MAXN*2];
int dist1[MAXN], head[MAXN], edgenum, s, dist2[MAXN];
int ans;
bool vis[MAXN];
void init() {
    memset(head, -1, sizeof(head));
    edgenum = 0;
}
void addEdge(int x, int y, int z) {
    edge[edgenum].from = x;
    edge[edgenum].to = y;
    edge[edgenum].val = z;
    edge[edgenum].next = head[x];
    head[x] = edgenum++;
}
void bfs1(int x) {
    queue<int> que; ans = 0;
    memset(vis, false, sizeof(vis));
    memset(dist1, 0, sizeof(dist1));
    while (!que.empty()) que.pop();
    que.push(x); vis[x] = true;
    while (que.size()) {
        int a = que.front(); que.pop();
        for (int i = head[a]; i != -1; i = edge[i].next) {
            int b = edge[i].to;
            if (!vis[b] && dist1[b] < dist1[a] + edge[i].val) {
                dist1[b] = dist1[a] + edge[i].val;
                if(ans < dist1[b]) {
                    ans = dist1[b]; s = b;
                }
                vis[b] = true; que.push(b);
            }
        }
    }
}void bfs2(int x) {
    queue<int> que; ans = 0;
    memset(vis, false, sizeof(vis));
    memset(dist2, 0, sizeof(dist2));
    while (!que.empty()) que.pop();
    que.push(x); vis[x] = true;
    while (que.size()) {
        int a = que.front(); que.pop();
        for (int i = head[a]; i != -1; i = edge[i].next) {
            int b = edge[i].to;
            if (!vis[b] && dist2[b] < dist2[a] + edge[i].val) {
                dist2[b] = dist2[a] + edge[i].val;
                if(ans < dist2[b]) {
                    ans = dist2[b]; s = b;
                }
                vis[b] = true; que.push(b);
            }
        }
    }
}
int main() {
    int a, b, c, n, m;
    while (scanf("%d", &n) != EOF) {
        init();
        for (int i = 2; i <= n; i++) {
            scanf("%d%d", &a, &b);
            addEdge(i, a, b); addEdge(a, i, b);
        }
        bfs1(1); bfs1(s); bfs2(s);
        for (int i = 1; i <= n; i++) {
            printf("%d
", max(dist1[i], dist2[i]));
        }
    }
    return 0;
}


原文地址:https://www.cnblogs.com/cniwoq/p/6770869.html