Programming thinking week6 homework A-computer network cable

topic

There was a computer (number 1) in the laboratory. Recently, Kryptonian Kurudong purchased N-1 computers for the laboratory, numbered 2 to N. Each computer is connected to a previously installed computer with a network cable. However, Gu Gudong is worried that the network speed is too slow. He wants to know the maximum cable length from the i-th computer to other computers.
Insert picture description here
Tip: The sample input corresponds to this figure. From this figure, you can see that the computer farthest from computer 1 is computer 4, and the distance between them is 3. Computer No. 4 and Computer No. 5 are the farthest points from Computer No. 2, so the answer is 2. Computer No. 5 is the farthest away from Computer No. 3, so for Computer No. 3, its answer is 3. Similarly, we can calculate that the answer for computer 4 and computer 5 is 4.

Input

The input file contains multiple sets of test data. For each set of test data, the first line is an integer N (N <= 10000), then there are N-1 lines, two numbers in each line, and for the two numbers in the i-th line, they represent the number connected to computer i The computer number and the length of the network cable between them. The total length of the network cable will not exceed 10 ^ 9, and each number is separated by a space.

Output

For each set of test data, N lines are output, and the i-th line represents the answer of computer i (1 <= i <= N).

Sample Input

5
1 1
2 1
3 1
1 1

Sample Output

3
2
3
4
4

Ideas

Hypothetical treeThe two leaf nodes of the longest way are v1, v2The farthest point from any point u must be the point in (v1, v2),after thatThe farthest point from v1 or v2 must be v2 or v1. Thus, after two traversals, the longest path can be found.
This question is not to find the longest path, but to find the longest path that a certain node x can reach. First thisThe longest path of node x is either the path to v1 or the path to v2.
The first dfs, choose a littlein, Find the point farthest from this point and record it as v1.
The second dfs, select pointsv1, Find the farthest point from this point and record it as v2.
The third dfs, pick pointsv1, Record the distance from each point in the graph to that point.
Fourth dfs, select pointv2, Record the distance from each point in the graph to that point.
The larger of the two distances obtained at each point is the desired one. You can keep following the new length in the third and fourth dfs, length [g [u] [i] .v] = max (length [g [u] [i] .v], len + g [u] [i ] .w).

Code

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct edge{
    int v,w;
    edge(){v=0;w=0;}
};
vector<edge>g[10005];
int maxlen=0,p,n;
int length[10005];
bool vis[10005];
void dfs(int u,int len){
    bool flag=false;
    for(int i=0;i<g[u].size();i++){
        if(!vis[g[u][i].v])
            flag=true;
    }
    if(flag==false){//到尽头
        if(len>=maxlen){
            maxlen=len;
            p=u;
        }
        return;
    }
    for(int i=0;i<g[u].size();i++){
        if(!vis[g[u][i].v]){//未到达过
            vis[g[u][i].v]=true;
            dfs(g[u][i].v, len+g[u][i].w);
        }
    }
}
void dfs1(int u,int len){
    for(int i=0;i<g[u].size();i++){
        if(!vis[g[u][i].v]){//未到达过
            vis[g[u][i].v]=true;
            length[g[u][i].v]=max(length[g[u][i].v],len+g[u][i].w);
            dfs1(g[u][i].v, len+g[u][i].w);
        }
    }
}

int main(){
    while (scanf("%d",&n)!=EOF) {
        memset(vis, false, sizeof(vis));
        for(int i=0;i<n;i++){
            length[i]=0;
            g[i].clear();
        }
        for(int i=2;i<=n;i++){
            edge e1,e2;
            scanf("%d %d",&e1.v,&e1.w);
            g[i].push_back(e1);
            e2.v=i;e2.w=e1.w;
            g[e1.v].push_back(e2);
        }
        int p1,p2;
        maxlen=0;
        vis[1]=true;dfs(1,0);
        memset(vis, false, sizeof(vis));
        p1=p;
        maxlen=0;
        vis[p]=true;dfs(p, 0);
        memset(vis, false, sizeof(vis));
        p2=p;
        vis[p1]=true;dfs1(p1,0);
        memset(vis, false, sizeof(vis));
        vis[p2]=true;dfs1(p2,0);
        for(int i=1;i<=n;i++){
            printf("%d\n",length[i]);
        }
    }
    return 0;
}

to sum up

The second dfs and the third dfs have the same starting point, and can be combined into one, then only three dfs can be obtained.
You can write only one dfs and maintain the ans [] array each time.
Title link

Published 24 original articles · praised 2 · visits 435

Guess you like

Origin blog.csdn.net/weixin_43805228/article/details/105174930